您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MqttKeepAliveMonitor_Tests.cs 3.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. }