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.8 KiB

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