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.
 
 
 
 

171 lines
6.8 KiB

  1. using System.Linq;
  2. using System.Threading.Tasks;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using MQTTnet.Client;
  5. using MQTTnet.Formatter;
  6. using MQTTnet.Protocol;
  7. namespace MQTTnet.Tests.Server
  8. {
  9. [TestClass]
  10. public sealed class Retained_Messages_Tests : BaseTestClass
  11. {
  12. [TestMethod]
  13. public async Task Server_Reports_Retained_Messages_Supported_V3()
  14. {
  15. using (var testEnvironment = CreateTestEnvironment())
  16. {
  17. await testEnvironment.StartServer();
  18. var client = testEnvironment.CreateClient();
  19. var connectResult = await client.ConnectAsync(testEnvironment.Factory.CreateClientOptionsBuilder()
  20. .WithProtocolVersion(MqttProtocolVersion.V311)
  21. .WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build());
  22. Assert.IsTrue(connectResult.RetainAvailable);
  23. }
  24. }
  25. [TestMethod]
  26. public async Task Server_Reports_Retained_Messages_Supported_V5()
  27. {
  28. using (var testEnvironment = CreateTestEnvironment(MqttProtocolVersion.V500))
  29. {
  30. await testEnvironment.StartServer();
  31. var client = testEnvironment.CreateClient();
  32. var connectResult = await client.ConnectAsync(testEnvironment.Factory.CreateClientOptionsBuilder()
  33. .WithProtocolVersion(MqttProtocolVersion.V500)
  34. .WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build());
  35. Assert.IsTrue(connectResult.RetainAvailable);
  36. }
  37. }
  38. [TestMethod]
  39. public async Task Retained_Messages_Flow()
  40. {
  41. using (var testEnvironment = CreateTestEnvironment())
  42. {
  43. var retainedMessage = new MqttApplicationMessageBuilder().WithTopic("r").WithPayload("r").WithRetainFlag().Build();
  44. await testEnvironment.StartServer();
  45. var c1 = await testEnvironment.ConnectClient();
  46. var c2 = await testEnvironment.ConnectClient();
  47. var messageHandler = testEnvironment.CreateApplicationMessageHandler(c2);
  48. await c1.PublishAsync(retainedMessage);
  49. await c1.DisconnectAsync();
  50. await LongTestDelay();
  51. for (var i = 0; i < 5; i++)
  52. {
  53. await c2.UnsubscribeAsync("r");
  54. await Task.Delay(100);
  55. messageHandler.AssertReceivedCountEquals(i);
  56. await c2.SubscribeAsync("r");
  57. await Task.Delay(100);
  58. messageHandler.AssertReceivedCountEquals(i + 1);
  59. }
  60. await c2.DisconnectAsync();
  61. }
  62. }
  63. [TestMethod]
  64. public async Task Receive_No_Retained_Message_After_Subscribe()
  65. {
  66. using (var testEnvironment = CreateTestEnvironment())
  67. {
  68. await testEnvironment.StartServer();
  69. var c1 = await testEnvironment.ConnectClient();
  70. await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
  71. await c1.DisconnectAsync();
  72. var c2 = await testEnvironment.ConnectClient();
  73. var messageHandler = testEnvironment.CreateApplicationMessageHandler(c2);
  74. await c2.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic("retained_other").Build());
  75. await Task.Delay(500);
  76. messageHandler.AssertReceivedCountEquals(0);
  77. }
  78. }
  79. [TestMethod]
  80. public async Task Receive_Retained_Message_After_Subscribe()
  81. {
  82. using (var testEnvironment = CreateTestEnvironment())
  83. {
  84. await testEnvironment.StartServer();
  85. var c1 = await testEnvironment.ConnectClient();
  86. await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
  87. await c1.DisconnectAsync();
  88. var c2 = await testEnvironment.ConnectClient();
  89. var messageHandler = testEnvironment.CreateApplicationMessageHandler(c2);
  90. await c2.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic("retained").Build());
  91. await Task.Delay(500);
  92. messageHandler.AssertReceivedCountEquals(1);
  93. Assert.IsTrue(messageHandler.ReceivedApplicationMessages.First().Retain);
  94. }
  95. }
  96. [TestMethod]
  97. public async Task Clear_Retained_Message_With_Empty_Payload()
  98. {
  99. using (var testEnvironment = CreateTestEnvironment())
  100. {
  101. await testEnvironment.StartServer();
  102. var c1 = await testEnvironment.ConnectClient();
  103. await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
  104. await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[0]).WithRetainFlag().Build());
  105. await c1.DisconnectAsync();
  106. var c2 = await testEnvironment.ConnectClient();
  107. var messageHandler = testEnvironment.CreateApplicationMessageHandler(c2);
  108. await Task.Delay(200);
  109. await c2.SubscribeAsync(new MqttTopicFilter { Topic = "retained", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
  110. await Task.Delay(500);
  111. messageHandler.AssertReceivedCountEquals(0);
  112. }
  113. }
  114. [TestMethod]
  115. public async Task Clear_Retained_Message_With_Null_Payload()
  116. {
  117. using (var testEnvironment = CreateTestEnvironment())
  118. {
  119. await testEnvironment.StartServer();
  120. var c1 = await testEnvironment.ConnectClient();
  121. await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
  122. await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload((byte[])null).WithRetainFlag().Build());
  123. await c1.DisconnectAsync();
  124. var c2 = await testEnvironment.ConnectClient();
  125. var messageHandler = testEnvironment.CreateApplicationMessageHandler(c2);
  126. await Task.Delay(200);
  127. await c2.SubscribeAsync(new MqttTopicFilter { Topic = "retained", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
  128. await Task.Delay(500);
  129. messageHandler.AssertReceivedCountEquals(0);
  130. }
  131. }
  132. }
  133. }