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.

MqttKeepAliveMonitorTests.cs 1.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using MQTTnet.Diagnostics;
  5. using MQTTnet.Packets;
  6. using MQTTnet.Server;
  7. namespace MQTTnet.Core.Tests
  8. {
  9. [TestClass]
  10. public class MqttKeepAliveMonitorTests
  11. {
  12. [TestMethod]
  13. public void KeepAlive_Timeout()
  14. {
  15. var timeoutCalledCount = 0;
  16. var monitor = new MqttClientKeepAliveMonitor(string.Empty, delegate
  17. {
  18. timeoutCalledCount++;
  19. return Task.FromResult(0);
  20. }, new MqttNetLogger());
  21. Assert.AreEqual(0, timeoutCalledCount);
  22. monitor.Start(1, CancellationToken.None);
  23. Assert.AreEqual(0, timeoutCalledCount);
  24. Thread.Sleep(2000); // Internally the keep alive timeout is multiplied with 1.5 as per protocol specification.
  25. Assert.AreEqual(1, timeoutCalledCount);
  26. }
  27. [TestMethod]
  28. public void KeepAlive_NoTimeout()
  29. {
  30. var timeoutCalledCount = 0;
  31. var monitor = new MqttClientKeepAliveMonitor(string.Empty, delegate
  32. {
  33. timeoutCalledCount++;
  34. return Task.FromResult(0);
  35. }, new MqttNetLogger());
  36. Assert.AreEqual(0, timeoutCalledCount);
  37. monitor.Start(1, CancellationToken.None);
  38. Assert.AreEqual(0, timeoutCalledCount);
  39. // Simulate traffic.
  40. Thread.Sleep(1000); // Internally the keep alive timeout is multiplied with 1.5 as per protocol specification.
  41. monitor.PacketReceived(new MqttPublishPacket());
  42. Thread.Sleep(1000);
  43. monitor.PacketReceived(new MqttPublishPacket());
  44. Thread.Sleep(1000);
  45. Assert.AreEqual(0, timeoutCalledCount);
  46. Thread.Sleep(2000);
  47. Assert.AreEqual(1, timeoutCalledCount);
  48. }
  49. }
  50. }