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.
 
 
 
 

64 lines
1.8 KiB

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