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.
 
 
 
 

279 regels
10 KiB

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