Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

162 řádky
5.0 KiB

  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Threading.Tasks;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using MQTTnet.Core.Adapter;
  6. using MQTTnet.Core.Client;
  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 async Task MqttServer_PublishSimple_AtMostOnce()
  17. {
  18. await TestPublishAsync(
  19. "A/B/C",
  20. MqttQualityOfServiceLevel.AtMostOnce,
  21. "A/B/C",
  22. MqttQualityOfServiceLevel.AtMostOnce,
  23. 1);
  24. }
  25. [TestMethod]
  26. public async Task MqttServer_PublishSimple_AtLeastOnce()
  27. {
  28. await TestPublishAsync(
  29. "A/B/C",
  30. MqttQualityOfServiceLevel.AtLeastOnce,
  31. "A/B/C",
  32. MqttQualityOfServiceLevel.AtLeastOnce,
  33. 1);
  34. }
  35. [TestMethod]
  36. public async Task MqttServer_PublishSimple_ExactlyOnce()
  37. {
  38. await TestPublishAsync(
  39. "A/B/C",
  40. MqttQualityOfServiceLevel.ExactlyOnce,
  41. "A/B/C",
  42. MqttQualityOfServiceLevel.ExactlyOnce,
  43. 1);
  44. }
  45. [TestMethod]
  46. public async Task MqttServer_WillMessage()
  47. {
  48. var s = new MqttServer(new MqttServerOptions(), new TestMqttServerAdapter());
  49. s.Start();
  50. var willMessage = new MqttApplicationMessage("My/last/will", new byte[0], MqttQualityOfServiceLevel.AtMostOnce, false);
  51. var c1 = ConnectTestClient("c1", null, s);
  52. var c2 = ConnectTestClient("c2", willMessage, s);
  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. s.Stop();
  59. Assert.AreEqual(1, receivedMessagesCount);
  60. }
  61. private MqttClient ConnectTestClient(string clientId, MqttApplicationMessage willMessage, MqttServer server)
  62. {
  63. var adapterA = new TestMqttClientAdapter();
  64. var adapterB = new TestMqttClientAdapter();
  65. adapterA.Partner = adapterB;
  66. adapterB.Partner = adapterA;
  67. var client = new MqttClient(new MqttClientOptions(), adapterA);
  68. server.InjectClient(clientId, adapterB);
  69. client.ConnectAsync(willMessage).Wait();
  70. return client;
  71. }
  72. private async Task TestPublishAsync(
  73. string topic,
  74. MqttQualityOfServiceLevel qualityOfServiceLevel,
  75. string topicFilter,
  76. MqttQualityOfServiceLevel filterQualityOfServiceLevel,
  77. int expectedReceivedMessagesCount)
  78. {
  79. var s = new MqttServer(new MqttServerOptions(), new TestMqttServerAdapter());
  80. s.Start();
  81. var c1 = ConnectTestClient("c1", null, s);
  82. var c2 = ConnectTestClient("c2", null, s);
  83. var receivedMessagesCount = 0;
  84. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  85. await c1.SubscribeAsync(new TopicFilter(topicFilter, filterQualityOfServiceLevel));
  86. await c2.PublishAsync(new MqttApplicationMessage(topic, new byte[0], qualityOfServiceLevel, false));
  87. await Task.Delay(500);
  88. await c1.Unsubscribe(topicFilter);
  89. await Task.Delay(500);
  90. s.Stop();
  91. Assert.AreEqual(expectedReceivedMessagesCount, receivedMessagesCount);
  92. }
  93. }
  94. public class TestMqttClientAdapter : IMqttCommunicationAdapter
  95. {
  96. private readonly BlockingCollection<MqttBasePacket> _incomingPackets = new BlockingCollection<MqttBasePacket>();
  97. public TestMqttClientAdapter Partner { get; set; }
  98. public async Task ConnectAsync(MqttClientOptions options, TimeSpan timeout)
  99. {
  100. await Task.FromResult(0);
  101. }
  102. public async Task DisconnectAsync()
  103. {
  104. await Task.FromResult(0);
  105. }
  106. public async Task SendPacketAsync(MqttBasePacket packet, TimeSpan timeout)
  107. {
  108. ThrowIfPartnerIsNull();
  109. Partner.SendPacketInternal(packet);
  110. await Task.FromResult(0);
  111. }
  112. public async Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout)
  113. {
  114. ThrowIfPartnerIsNull();
  115. return await Task.Run(() => _incomingPackets.Take());
  116. }
  117. private void SendPacketInternal(MqttBasePacket packet)
  118. {
  119. if (packet == null) throw new ArgumentNullException(nameof(packet));
  120. _incomingPackets.Add(packet);
  121. }
  122. private void ThrowIfPartnerIsNull()
  123. {
  124. if (Partner == null)
  125. {
  126. throw new InvalidOperationException("Partner is not set.");
  127. }
  128. }
  129. }
  130. }