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.
 
 
 
 

160 lines
5.9 KiB

  1. using System.Linq;
  2. using System.Threading.Tasks;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using MQTTnet.Client;
  5. using MQTTnet.Client.Options;
  6. using MQTTnet.Protocol;
  7. using MQTTnet.Server;
  8. using MQTTnet.Tests.Mockups;
  9. namespace MQTTnet.Tests.Server
  10. {
  11. [TestClass]
  12. public sealed class Status_Tests : BaseTestClass
  13. {
  14. [TestMethod]
  15. public async Task Show_Client_And_Session_Statistics()
  16. {
  17. using (var testEnvironment = new TestEnvironment(TestContext))
  18. {
  19. var server = await testEnvironment.StartServer();
  20. var c1 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithClientId("client1"));
  21. var c2 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithClientId("client2"));
  22. await Task.Delay(500);
  23. var clientStatus = await server.GetClientStatusAsync();
  24. var sessionStatus = await server.GetSessionStatusAsync();
  25. Assert.AreEqual(2, clientStatus.Count);
  26. Assert.AreEqual(2, sessionStatus.Count);
  27. Assert.IsTrue(clientStatus.Any(s => s.ClientId == c1.Options.ClientId));
  28. Assert.IsTrue(clientStatus.Any(s => s.ClientId == c2.Options.ClientId));
  29. await c1.DisconnectAsync();
  30. await c2.DisconnectAsync();
  31. await Task.Delay(500);
  32. clientStatus = await server.GetClientStatusAsync();
  33. sessionStatus = await server.GetSessionStatusAsync();
  34. Assert.AreEqual(0, clientStatus.Count);
  35. Assert.AreEqual(0, sessionStatus.Count);
  36. }
  37. }
  38. [TestMethod]
  39. public async Task Disconnect_Client()
  40. {
  41. using (var testEnvironment = new TestEnvironment(TestContext))
  42. {
  43. var server = await testEnvironment.StartServer();
  44. var c1 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithClientId("client1"));
  45. await Task.Delay(1000);
  46. var clientStatus = await server.GetClientStatusAsync();
  47. Assert.AreEqual(1, clientStatus.Count);
  48. Assert.IsTrue(clientStatus.Any(s => s.ClientId == c1.Options.ClientId));
  49. await clientStatus.First().DisconnectAsync();
  50. await Task.Delay(500);
  51. Assert.IsFalse(c1.IsConnected);
  52. clientStatus = await server.GetClientStatusAsync();
  53. Assert.AreEqual(0, clientStatus.Count);
  54. }
  55. }
  56. [TestMethod]
  57. public async Task Keep_Persistent_Session()
  58. {
  59. using (var testEnvironment = new TestEnvironment(TestContext))
  60. {
  61. var server = await testEnvironment.StartServer(new MqttServerOptionsBuilder().WithPersistentSessions());
  62. var c1 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithClientId("client1").WithCleanSession(false));
  63. var c2 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithClientId("client2").WithCleanSession(false));
  64. await c1.DisconnectAsync();
  65. await Task.Delay(500);
  66. var clientStatus = await server.GetClientStatusAsync();
  67. var sessionStatus = await server.GetSessionStatusAsync();
  68. Assert.AreEqual(1, clientStatus.Count);
  69. Assert.AreEqual(2, sessionStatus.Count);
  70. await c2.DisconnectAsync();
  71. await Task.Delay(500);
  72. clientStatus = await server.GetClientStatusAsync();
  73. sessionStatus = await server.GetSessionStatusAsync();
  74. Assert.AreEqual(0, clientStatus.Count);
  75. Assert.AreEqual(2, sessionStatus.Count);
  76. }
  77. }
  78. [TestMethod]
  79. public async Task Track_Sent_Application_Messages()
  80. {
  81. using (var testEnvironment = new TestEnvironment(TestContext))
  82. {
  83. var server = await testEnvironment.StartServer(new MqttServerOptionsBuilder().WithPersistentSessions());
  84. var c1 = await testEnvironment.ConnectClient();
  85. for (var i = 1; i < 25; i++)
  86. {
  87. await c1.PublishAsync("a");
  88. await Task.Delay(50);
  89. var clientStatus = await server.GetClientStatusAsync();
  90. Assert.AreEqual(i, clientStatus.First().SentApplicationMessagesCount);
  91. Assert.AreEqual(0, clientStatus.First().ReceivedApplicationMessagesCount);
  92. }
  93. }
  94. }
  95. [TestMethod]
  96. public async Task Track_Sent_Packets()
  97. {
  98. using (var testEnvironment = new TestEnvironment(TestContext))
  99. {
  100. var server = await testEnvironment.StartServer(new MqttServerOptionsBuilder().WithPersistentSessions());
  101. var c1 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithNoKeepAlive());
  102. for (var i = 1; i < 25; i++)
  103. {
  104. // At most once will send one packet to the client and the server will reply
  105. // with an additional ACK packet.
  106. await c1.PublishAsync("a", string.Empty, MqttQualityOfServiceLevel.AtLeastOnce);
  107. await Task.Delay(250);
  108. var clientStatus = await server.GetClientStatusAsync();
  109. Assert.AreEqual(i, clientStatus.First().SentApplicationMessagesCount, "SAMC invalid!");
  110. // + 1 because CONNECT is also counted.
  111. Assert.AreEqual(i + 1, clientStatus.First().SentPacketsCount, "SPC invalid!");
  112. // +1 because ConnACK package is already counted.
  113. Assert.AreEqual(i + 1, clientStatus.First().ReceivedPacketsCount, "RPC invalid!");
  114. }
  115. }
  116. }
  117. }
  118. }