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 3.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using MQTTnet.Adapter;
  7. using MQTTnet.Diagnostics;
  8. using MQTTnet.Packets;
  9. using MQTTnet.Server;
  10. namespace MQTTnet.Core.Tests
  11. {
  12. [TestClass]
  13. public class MqttKeepAliveMonitorTests
  14. {
  15. [TestMethod]
  16. public void KeepAlive_Timeout()
  17. {
  18. var clientSession = new TestClientSession();
  19. var monitor = new MqttClientKeepAliveMonitor(clientSession, new MqttNetLogger().CreateChildLogger());
  20. Assert.AreEqual(0, clientSession.StopCalledCount);
  21. monitor.Start(1, CancellationToken.None);
  22. Assert.AreEqual(0, clientSession.StopCalledCount);
  23. Thread.Sleep(2000); // Internally the keep alive timeout is multiplied with 1.5 as per protocol specification.
  24. Assert.AreEqual(1, clientSession.StopCalledCount);
  25. }
  26. [TestMethod]
  27. public void KeepAlive_NoTimeout()
  28. {
  29. var clientSession = new TestClientSession();
  30. var monitor = new MqttClientKeepAliveMonitor(clientSession, new MqttNetLogger().CreateChildLogger());
  31. Assert.AreEqual(0, clientSession.StopCalledCount);
  32. monitor.Start(1, CancellationToken.None);
  33. Assert.AreEqual(0, clientSession.StopCalledCount);
  34. // Simulate traffic.
  35. Thread.Sleep(1000); // Internally the keep alive timeout is multiplied with 1.5 as per protocol specification.
  36. monitor.PacketReceived(new MqttPublishPacket());
  37. Thread.Sleep(1000);
  38. monitor.PacketReceived(new MqttPublishPacket());
  39. Thread.Sleep(1000);
  40. Assert.AreEqual(0, clientSession.StopCalledCount);
  41. Thread.Sleep(2000);
  42. Assert.AreEqual(1, clientSession.StopCalledCount);
  43. }
  44. private class TestClientSession : IMqttClientSession
  45. {
  46. public string ClientId { get; }
  47. public int StopCalledCount { get; private set; }
  48. public void FillStatus(MqttClientSessionStatus status)
  49. {
  50. throw new NotSupportedException();
  51. }
  52. public void EnqueueApplicationMessage(MqttClientSession senderClientSession, MqttPublishPacket publishPacket)
  53. {
  54. throw new NotSupportedException();
  55. }
  56. public void ClearPendingApplicationMessages()
  57. {
  58. throw new NotSupportedException();
  59. }
  60. public Task RunAsync(MqttConnectPacket connectPacket, IMqttChannelAdapter adapter)
  61. {
  62. throw new NotSupportedException();
  63. }
  64. public void Stop(MqttClientDisconnectType disconnectType)
  65. {
  66. StopCalledCount++;
  67. }
  68. public Task SubscribeAsync(IList<TopicFilter> topicFilters)
  69. {
  70. throw new NotSupportedException();
  71. }
  72. public Task UnsubscribeAsync(IList<string> topicFilters)
  73. {
  74. throw new NotSupportedException();
  75. }
  76. public void Dispose()
  77. {
  78. }
  79. }
  80. }
  81. }