Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Client_Tests.cs 11 KiB

5 anos atrás
5 anos atrás
5 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. using MQTTnet.Tests.Mockups;
  14. namespace MQTTnet.Tests.MQTTv5
  15. {
  16. [TestClass]
  17. public class Client_Tests
  18. {
  19. [TestMethod]
  20. public async Task Connect_With_New_Mqtt_Features()
  21. {
  22. using (var testEnvironment = new TestEnvironment())
  23. {
  24. await testEnvironment.StartServerAsync();
  25. // This test can be also executed against "broker.hivemq.com" to validate package format.
  26. var client = await testEnvironment.ConnectClientAsync(
  27. new MqttClientOptionsBuilder()
  28. //.WithTcpServer("broker.hivemq.com")
  29. .WithTcpServer("127.0.0.1", testEnvironment.ServerPort)
  30. .WithProtocolVersion(MqttProtocolVersion.V500)
  31. .WithTopicAliasMaximum(20)
  32. .WithReceiveMaximum(20)
  33. .WithWillMessage(new MqttApplicationMessageBuilder().WithTopic("abc").Build())
  34. .WithWillDelayInterval(20)
  35. .Build());
  36. MqttApplicationMessage receivedMessage = null;
  37. await client.SubscribeAsync("a");
  38. client.UseApplicationMessageReceivedHandler(context => { receivedMessage = context.ApplicationMessage; });
  39. await client.PublishAsync(new MqttApplicationMessageBuilder()
  40. .WithTopic("a")
  41. .WithPayload("x")
  42. .WithUserProperty("a", "1")
  43. .WithUserProperty("b", "2")
  44. .WithPayloadFormatIndicator(MqttPayloadFormatIndicator.CharacterData)
  45. .WithAtLeastOnceQoS()
  46. .Build());
  47. await Task.Delay(500);
  48. Assert.IsNotNull(receivedMessage);
  49. Assert.AreEqual(2, receivedMessage.UserProperties.Count);
  50. }
  51. }
  52. [TestMethod]
  53. public async Task Connect()
  54. {
  55. var server = new MqttFactory().CreateMqttServer();
  56. var client = new MqttFactory().CreateMqttClient();
  57. try
  58. {
  59. await server.StartAsync(new MqttServerOptions());
  60. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  61. }
  62. finally
  63. {
  64. await server.StopAsync();
  65. }
  66. }
  67. [TestMethod]
  68. public async Task Connect_And_Disconnect()
  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.DisconnectAsync();
  77. }
  78. finally
  79. {
  80. await server.StopAsync();
  81. }
  82. }
  83. [TestMethod]
  84. public async Task Subscribe()
  85. {
  86. var server = new MqttFactory().CreateMqttServer();
  87. var client = new MqttFactory().CreateMqttClient();
  88. try
  89. {
  90. await server.StartAsync(new MqttServerOptions());
  91. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  92. var result = await client.SubscribeAsync(new MqttClientSubscribeOptions()
  93. {
  94. SubscriptionIdentifier = 1,
  95. TopicFilters = new List<TopicFilter>
  96. {
  97. new TopicFilter { Topic = "a", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce}
  98. }
  99. });
  100. await client.DisconnectAsync();
  101. Assert.AreEqual(1, result.Items.Count);
  102. Assert.AreEqual(MqttClientSubscribeResultCode.GrantedQoS1, result.Items[0].ResultCode);
  103. }
  104. finally
  105. {
  106. await server.StopAsync();
  107. }
  108. }
  109. [TestMethod]
  110. public async Task Unsubscribe()
  111. {
  112. var server = new MqttFactory().CreateMqttServer();
  113. var client = new MqttFactory().CreateMqttClient();
  114. try
  115. {
  116. await server.StartAsync(new MqttServerOptions());
  117. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  118. await client.SubscribeAsync("a");
  119. var result = await client.UnsubscribeAsync("a");
  120. await client.DisconnectAsync();
  121. Assert.AreEqual(1, result.Items.Count);
  122. Assert.AreEqual(MqttClientUnsubscribeResultCode.Success, result.Items[0].ReasonCode);
  123. }
  124. finally
  125. {
  126. await server.StopAsync();
  127. }
  128. }
  129. [TestMethod]
  130. public async Task Publish_QoS_0()
  131. {
  132. var server = new MqttFactory().CreateMqttServer();
  133. var client = new MqttFactory().CreateMqttClient();
  134. try
  135. {
  136. await server.StartAsync(new MqttServerOptions());
  137. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  138. var result = await client.PublishAsync("a", "b");
  139. await client.DisconnectAsync();
  140. Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
  141. }
  142. finally
  143. {
  144. await server.StopAsync();
  145. }
  146. }
  147. [TestMethod]
  148. public async Task Publish_QoS_1()
  149. {
  150. var server = new MqttFactory().CreateMqttServer();
  151. var client = new MqttFactory().CreateMqttClient();
  152. try
  153. {
  154. await server.StartAsync(new MqttServerOptions());
  155. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  156. var result = await client.PublishAsync("a", "b", MqttQualityOfServiceLevel.AtLeastOnce);
  157. await client.DisconnectAsync();
  158. Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
  159. }
  160. finally
  161. {
  162. await server.StopAsync();
  163. }
  164. }
  165. [TestMethod]
  166. public async Task Publish_QoS_2()
  167. {
  168. var server = new MqttFactory().CreateMqttServer();
  169. var client = new MqttFactory().CreateMqttClient();
  170. try
  171. {
  172. await server.StartAsync(new MqttServerOptions());
  173. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  174. var result = await client.PublishAsync("a", "b", MqttQualityOfServiceLevel.ExactlyOnce);
  175. await client.DisconnectAsync();
  176. Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
  177. }
  178. finally
  179. {
  180. await server.StopAsync();
  181. }
  182. }
  183. [TestMethod]
  184. public async Task Publish_With_Properties()
  185. {
  186. var server = new MqttFactory().CreateMqttServer();
  187. var client = new MqttFactory().CreateMqttClient();
  188. try
  189. {
  190. await server.StartAsync(new MqttServerOptions());
  191. var applicationMessage = new MqttApplicationMessageBuilder()
  192. .WithTopic("Hello")
  193. .WithPayload("World")
  194. .WithAtMostOnceQoS()
  195. .WithUserProperty("x", "1")
  196. .WithUserProperty("y", "2")
  197. .WithResponseTopic("response")
  198. .WithContentType("text")
  199. .WithMessageExpiryInterval(50)
  200. .WithCorrelationData(new byte[12])
  201. .WithTopicAlias(2)
  202. .Build();
  203. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  204. var result = await client.PublishAsync(applicationMessage);
  205. await client.DisconnectAsync();
  206. Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
  207. }
  208. finally
  209. {
  210. await server.StopAsync();
  211. }
  212. }
  213. [TestMethod]
  214. public async Task Subscribe_And_Publish()
  215. {
  216. var server = new MqttFactory().CreateMqttServer();
  217. var client1 = new MqttFactory().CreateMqttClient();
  218. var client2 = new MqttFactory().CreateMqttClient();
  219. try
  220. {
  221. await server.StartAsync(new MqttServerOptions());
  222. var receivedMessages = new List<MqttApplicationMessageReceivedEventArgs>();
  223. await client1.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithClientId("client1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  224. client1.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
  225. {
  226. lock (receivedMessages)
  227. {
  228. receivedMessages.Add(e);
  229. }
  230. });
  231. await client1.SubscribeAsync("a");
  232. await client2.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithClientId("client2").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  233. await client2.PublishAsync("a", "b");
  234. await Task.Delay(500);
  235. await client2.DisconnectAsync();
  236. await client1.DisconnectAsync();
  237. Assert.AreEqual(1, receivedMessages.Count);
  238. Assert.AreEqual("client1", receivedMessages[0].ClientId);
  239. Assert.AreEqual("a", receivedMessages[0].ApplicationMessage.Topic);
  240. Assert.AreEqual("b", receivedMessages[0].ApplicationMessage.ConvertPayloadToString());
  241. }
  242. finally
  243. {
  244. await server.StopAsync();
  245. }
  246. }
  247. }
  248. }