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.

преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using MQTTnet.Core.Adapter;
  5. using MQTTnet.Core.Client;
  6. using MQTTnet.Core.Packets;
  7. using MQTTnet.Core.Protocol;
  8. using MQTTnet.Core.Server;
  9. namespace MQTTnet.Core.Tests
  10. {
  11. [TestClass]
  12. public class MqttServerTests
  13. {
  14. [TestMethod]
  15. public void MqttServer_PublishSimple_AtMostOnce()
  16. {
  17. TestPublishAsync(
  18. "A/B/C",
  19. MqttQualityOfServiceLevel.AtMostOnce,
  20. "A/B/C",
  21. MqttQualityOfServiceLevel.AtMostOnce,
  22. 1).Wait();
  23. }
  24. [TestMethod]
  25. public void MqttServer_PublishSimple_AtLeastOnce()
  26. {
  27. TestPublishAsync(
  28. "A/B/C",
  29. MqttQualityOfServiceLevel.AtLeastOnce,
  30. "A/B/C",
  31. MqttQualityOfServiceLevel.AtLeastOnce,
  32. 1).Wait();
  33. }
  34. [TestMethod]
  35. public void MqttServer_PublishSimple_ExactlyOnce()
  36. {
  37. TestPublishAsync(
  38. "A/B/C",
  39. MqttQualityOfServiceLevel.ExactlyOnce,
  40. "A/B/C",
  41. MqttQualityOfServiceLevel.ExactlyOnce,
  42. 1).Wait();
  43. }
  44. [TestMethod]
  45. public async Task MqttServer_WillMessage()
  46. {
  47. var serverAdapter = new TestMqttServerAdapter();
  48. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter });
  49. await s.StartAsync();
  50. var willMessage = new MqttApplicationMessage("My/last/will", new byte[0], MqttQualityOfServiceLevel.AtMostOnce, false);
  51. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  52. var c2 = await serverAdapter.ConnectTestClient(s, "c2", willMessage);
  53. var receivedMessagesCount = 0;
  54. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  55. await c1.SubscribeAsync(new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce));
  56. await c2.DisconnectAsync();
  57. await Task.Delay(1000);
  58. await s.StopAsync();
  59. Assert.AreEqual(1, receivedMessagesCount);
  60. }
  61. [TestMethod]
  62. public async Task MqttServer_Unsubscribe()
  63. {
  64. var serverAdapter = new TestMqttServerAdapter();
  65. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter });
  66. await s.StartAsync();
  67. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  68. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  69. var receivedMessagesCount = 0;
  70. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  71. var message = new MqttApplicationMessage("a", new byte[0], MqttQualityOfServiceLevel.AtLeastOnce, false);
  72. await c2.PublishAsync(message);
  73. Assert.AreEqual(0, receivedMessagesCount);
  74. await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
  75. await c2.PublishAsync(message);
  76. await Task.Delay(500);
  77. Assert.AreEqual(1, receivedMessagesCount);
  78. await c1.UnsubscribeAsync("a");
  79. await c2.PublishAsync(message);
  80. await Task.Delay(500);
  81. Assert.AreEqual(1, receivedMessagesCount);
  82. await s.StopAsync();
  83. await Task.Delay(500);
  84. Assert.AreEqual(1, receivedMessagesCount);
  85. }
  86. [TestMethod]
  87. public async Task MqttServer_Publish()
  88. {
  89. var serverAdapter = new TestMqttServerAdapter();
  90. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter });
  91. await s.StartAsync();
  92. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  93. var receivedMessagesCount = 0;
  94. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  95. var message = new MqttApplicationMessage("a", new byte[0], MqttQualityOfServiceLevel.AtLeastOnce, false);
  96. await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
  97. s.Publish(message);
  98. await Task.Delay(500);
  99. await s.StopAsync();
  100. Assert.AreEqual(1, receivedMessagesCount);
  101. }
  102. [TestMethod]
  103. public async Task MqttServer_NoRetainedMessage()
  104. {
  105. var serverAdapter = new TestMqttServerAdapter();
  106. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter });
  107. await s.StartAsync();
  108. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  109. await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, false));
  110. await c1.DisconnectAsync();
  111. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  112. var receivedMessagesCount = 0;
  113. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  114. await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
  115. await Task.Delay(500);
  116. await s.StopAsync();
  117. Assert.AreEqual(0, receivedMessagesCount);
  118. }
  119. [TestMethod]
  120. public async Task MqttServer_RetainedMessage()
  121. {
  122. var serverAdapter = new TestMqttServerAdapter();
  123. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter });
  124. await s.StartAsync();
  125. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  126. await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, true));
  127. await c1.DisconnectAsync();
  128. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  129. var receivedMessagesCount = 0;
  130. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  131. await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
  132. await Task.Delay(500);
  133. await s.StopAsync();
  134. Assert.AreEqual(1, receivedMessagesCount);
  135. }
  136. [TestMethod]
  137. public async Task MqttServer_ClearRetainedMessage()
  138. {
  139. var serverAdapter = new TestMqttServerAdapter();
  140. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter });
  141. await s.StartAsync();
  142. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  143. await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, true));
  144. await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[0], MqttQualityOfServiceLevel.AtLeastOnce, true));
  145. await c1.DisconnectAsync();
  146. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  147. var receivedMessagesCount = 0;
  148. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  149. await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
  150. await Task.Delay(500);
  151. await s.StopAsync();
  152. Assert.AreEqual(0, receivedMessagesCount);
  153. }
  154. [TestMethod]
  155. public async Task MqttServer_PersistRetainedMessage()
  156. {
  157. var storage = new TestStorage();
  158. var serverAdapter = new TestMqttServerAdapter();
  159. var s = new MqttServer(new MqttServerOptions { Storage = storage }, new List<IMqttServerAdapter> { serverAdapter });
  160. await s.StartAsync();
  161. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  162. await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, true));
  163. await c1.DisconnectAsync();
  164. await s.StopAsync();
  165. s = new MqttServer(new MqttServerOptions { Storage = storage }, new List<IMqttServerAdapter> { serverAdapter });
  166. await s.StartAsync();
  167. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  168. var receivedMessagesCount = 0;
  169. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  170. await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
  171. await Task.Delay(500);
  172. await s.StopAsync();
  173. Assert.AreEqual(1, receivedMessagesCount);
  174. }
  175. private class TestStorage : IMqttServerStorage
  176. {
  177. private IList<MqttApplicationMessage> _messages = new List<MqttApplicationMessage>();
  178. public Task SaveRetainedMessagesAsync(IList<MqttApplicationMessage> messages)
  179. {
  180. _messages = messages;
  181. return Task.FromResult(0);
  182. }
  183. public Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
  184. {
  185. return Task.FromResult(_messages);
  186. }
  187. }
  188. private static async Task TestPublishAsync(
  189. string topic,
  190. MqttQualityOfServiceLevel qualityOfServiceLevel,
  191. string topicFilter,
  192. MqttQualityOfServiceLevel filterQualityOfServiceLevel,
  193. int expectedReceivedMessagesCount)
  194. {
  195. var serverAdapter = new TestMqttServerAdapter();
  196. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter });
  197. await s.StartAsync();
  198. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  199. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  200. var receivedMessagesCount = 0;
  201. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  202. await c1.SubscribeAsync(new TopicFilter(topicFilter, filterQualityOfServiceLevel));
  203. await c2.PublishAsync(new MqttApplicationMessage(topic, new byte[0], qualityOfServiceLevel, false));
  204. await Task.Delay(500);
  205. await c1.UnsubscribeAsync(topicFilter);
  206. await Task.Delay(500);
  207. await s.StopAsync();
  208. Assert.AreEqual(expectedReceivedMessagesCount, receivedMessagesCount);
  209. }
  210. }
  211. }