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.
 
 
 
 

108 lines
3.2 KiB

  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. using MQTTnet.Server.Status;
  11. namespace MQTTnet.Tests
  12. {
  13. [TestClass]
  14. public class MqttKeepAliveMonitorTests
  15. {
  16. [TestMethod]
  17. public void KeepAlive_Timeout()
  18. {
  19. var clientSession = new TestClientSession();
  20. var monitor = new MqttClientKeepAliveMonitor(clientSession, new MqttNetLogger().CreateChildLogger());
  21. Assert.AreEqual(0, clientSession.StopCalledCount);
  22. monitor.Start(1, CancellationToken.None);
  23. Assert.AreEqual(0, clientSession.StopCalledCount);
  24. Thread.Sleep(2000); // Internally the keep alive timeout is multiplied with 1.5 as per protocol specification.
  25. Assert.AreEqual(1, clientSession.StopCalledCount);
  26. }
  27. [TestMethod]
  28. public void KeepAlive_NoTimeout()
  29. {
  30. var clientSession = new TestClientSession();
  31. var monitor = new MqttClientKeepAliveMonitor(clientSession, new MqttNetLogger().CreateChildLogger());
  32. Assert.AreEqual(0, clientSession.StopCalledCount);
  33. monitor.Start(1, CancellationToken.None);
  34. Assert.AreEqual(0, clientSession.StopCalledCount);
  35. // Simulate traffic.
  36. Thread.Sleep(1000); // Internally the keep alive timeout is multiplied with 1.5 as per protocol specification.
  37. monitor.PacketReceived();
  38. Thread.Sleep(1000);
  39. monitor.PacketReceived();
  40. Thread.Sleep(1000);
  41. Assert.AreEqual(0, clientSession.StopCalledCount);
  42. Thread.Sleep(2000);
  43. Assert.AreEqual(1, clientSession.StopCalledCount);
  44. }
  45. private class TestClientSession : IMqttClientSession
  46. {
  47. public string ClientId { get; }
  48. public int StopCalledCount { get; private set; }
  49. public void FillStatus(MqttClientStatus status)
  50. {
  51. throw new NotSupportedException();
  52. }
  53. public void EnqueueApplicationMessage(MqttClientConnection senderClientSession, MqttApplicationMessage applicationMessage)
  54. {
  55. throw new NotSupportedException();
  56. }
  57. public void ClearPendingApplicationMessages()
  58. {
  59. throw new NotSupportedException();
  60. }
  61. public Task RunAsync(MqttApplicationMessage willMessage, int keepAlivePeriod, IMqttChannelAdapter adapter)
  62. {
  63. throw new NotSupportedException();
  64. }
  65. public Task StopAsync()
  66. {
  67. StopCalledCount++;
  68. return Task.FromResult(0);
  69. }
  70. public Task SubscribeAsync(IList<TopicFilter> topicFilters)
  71. {
  72. throw new NotSupportedException();
  73. }
  74. public Task UnsubscribeAsync(IList<string> topicFilters)
  75. {
  76. throw new NotSupportedException();
  77. }
  78. public void Dispose()
  79. {
  80. }
  81. }
  82. }
  83. }