No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

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