No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

MqttServerTests.cs 5.7 KiB

hace 7 años
hace 7 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 async Task MqttServer_PublishSimple_AtMostOnce()
  16. {
  17. await TestPublishAsync(
  18. "A/B/C",
  19. MqttQualityOfServiceLevel.AtMostOnce,
  20. "A/B/C",
  21. MqttQualityOfServiceLevel.AtMostOnce,
  22. 1);
  23. }
  24. [TestMethod]
  25. public async Task MqttServer_PublishSimple_AtLeastOnce()
  26. {
  27. await TestPublishAsync(
  28. "A/B/C",
  29. MqttQualityOfServiceLevel.AtLeastOnce,
  30. "A/B/C",
  31. MqttQualityOfServiceLevel.AtLeastOnce,
  32. 1);
  33. }
  34. [TestMethod]
  35. public async Task MqttServer_PublishSimple_ExactlyOnce()
  36. {
  37. await TestPublishAsync(
  38. "A/B/C",
  39. MqttQualityOfServiceLevel.ExactlyOnce,
  40. "A/B/C",
  41. MqttQualityOfServiceLevel.ExactlyOnce,
  42. 1);
  43. }
  44. [TestMethod]
  45. public async Task MqttServer_WillMessage()
  46. {
  47. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { new TestMqttServerAdapter() });
  48. s.Start();
  49. var willMessage = new MqttApplicationMessage("My/last/will", new byte[0], MqttQualityOfServiceLevel.AtMostOnce, false);
  50. var c1 = ConnectTestClient("c1", null, s);
  51. var c2 = ConnectTestClient("c2", willMessage, s);
  52. var receivedMessagesCount = 0;
  53. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  54. await c1.SubscribeAsync(new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce));
  55. await c2.DisconnectAsync();
  56. await Task.Delay(1000);
  57. s.Stop();
  58. Assert.AreEqual(1, receivedMessagesCount);
  59. }
  60. [TestMethod]
  61. public async Task MqttServer_Unsubscribe()
  62. {
  63. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { new TestMqttServerAdapter() });
  64. s.Start();
  65. var c1 = ConnectTestClient("c1", null, s);
  66. var c2 = ConnectTestClient("c2", null, s);
  67. var receivedMessagesCount = 0;
  68. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  69. var message = new MqttApplicationMessage("a", new byte[0], MqttQualityOfServiceLevel.AtLeastOnce, false);
  70. await c2.PublishAsync(message);
  71. Assert.AreEqual(0, receivedMessagesCount);
  72. await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
  73. await c2.PublishAsync(message);
  74. await Task.Delay(500);
  75. Assert.AreEqual(1, receivedMessagesCount);
  76. await c1.Unsubscribe("a");
  77. await c2.PublishAsync(message);
  78. await Task.Delay(500);
  79. Assert.AreEqual(1, receivedMessagesCount);
  80. s.Stop();
  81. await Task.Delay(500);
  82. Assert.AreEqual(1, receivedMessagesCount);
  83. }
  84. [TestMethod]
  85. public async Task MqttServer_Publish()
  86. {
  87. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { new TestMqttServerAdapter() });
  88. s.Start();
  89. var c1 = ConnectTestClient("c1", null, s);
  90. var receivedMessagesCount = 0;
  91. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  92. var message = new MqttApplicationMessage("a", new byte[0], MqttQualityOfServiceLevel.AtLeastOnce, false);
  93. await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
  94. s.Publish(message);
  95. await Task.Delay(500);
  96. s.Stop();
  97. Assert.AreEqual(1, receivedMessagesCount);
  98. }
  99. private static MqttClient ConnectTestClient(string clientId, MqttApplicationMessage willMessage, MqttServer server)
  100. {
  101. var adapterA = new TestMqttCommunicationAdapter();
  102. var adapterB = new TestMqttCommunicationAdapter();
  103. adapterA.Partner = adapterB;
  104. adapterB.Partner = adapterA;
  105. var client = new MqttClient(new MqttClientOptions(), adapterA);
  106. server.InjectClient(clientId, adapterB);
  107. client.ConnectAsync(willMessage).Wait();
  108. return client;
  109. }
  110. private async Task TestPublishAsync(
  111. string topic,
  112. MqttQualityOfServiceLevel qualityOfServiceLevel,
  113. string topicFilter,
  114. MqttQualityOfServiceLevel filterQualityOfServiceLevel,
  115. int expectedReceivedMessagesCount)
  116. {
  117. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { new TestMqttServerAdapter() });
  118. s.Start();
  119. var c1 = ConnectTestClient("c1", null, s);
  120. var c2 = ConnectTestClient("c2", null, s);
  121. var receivedMessagesCount = 0;
  122. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  123. await c1.SubscribeAsync(new TopicFilter(topicFilter, filterQualityOfServiceLevel));
  124. await c2.PublishAsync(new MqttApplicationMessage(topic, new byte[0], qualityOfServiceLevel, false));
  125. await Task.Delay(500);
  126. await c1.Unsubscribe(topicFilter);
  127. await Task.Delay(500);
  128. s.Stop();
  129. Assert.AreEqual(expectedReceivedMessagesCount, receivedMessagesCount);
  130. }
  131. }
  132. }