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ů.
 
 
 
 

164 řádky
6.0 KiB

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