Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

106 řádky
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. 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; 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<bool> 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. }