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.
 
 
 
 

107 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.Server;
  9. using MQTTnet.Server.Status;
  10. namespace MQTTnet.Tests
  11. {
  12. [TestClass]
  13. public class MqttKeepAliveMonitor_Tests
  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();
  37. Thread.Sleep(1000);
  38. monitor.PacketReceived();
  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(MqttClientStatus status)
  49. {
  50. throw new NotSupportedException();
  51. }
  52. public void EnqueueApplicationMessage(MqttClientConnection senderClientSession, MqttApplicationMessage applicationMessage)
  53. {
  54. throw new NotSupportedException();
  55. }
  56. public void ClearPendingApplicationMessages()
  57. {
  58. throw new NotSupportedException();
  59. }
  60. public Task RunAsync(MqttApplicationMessage willMessage, int keepAlivePeriod, IMqttChannelAdapter adapter)
  61. {
  62. throw new NotSupportedException();
  63. }
  64. public Task StopAsync()
  65. {
  66. StopCalledCount++;
  67. return Task.FromResult(0);
  68. }
  69. public Task SubscribeAsync(IList<TopicFilter> topicFilters)
  70. {
  71. throw new NotSupportedException();
  72. }
  73. public Task UnsubscribeAsync(IList<string> topicFilters)
  74. {
  75. throw new NotSupportedException();
  76. }
  77. public void Dispose()
  78. {
  79. }
  80. }
  81. }
  82. }