|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using System.Threading;
- using System.Threading.Tasks;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using MQTTnet.Diagnostics;
- using MQTTnet.Packets;
- using MQTTnet.Server;
-
- namespace MQTTnet.Core.Tests
- {
- [TestClass]
- public class MqttKeepAliveMonitorTests
- {
- [TestMethod]
- public void KeepAlive_Timeout()
- {
- var timeoutCalledCount = 0;
-
- var monitor = new MqttClientKeepAliveMonitor(string.Empty, delegate
- {
- timeoutCalledCount++;
- return Task.FromResult(0);
- }, new MqttNetLogger());
-
- Assert.AreEqual(0, timeoutCalledCount);
-
- monitor.Start(1, CancellationToken.None);
-
- Assert.AreEqual(0, timeoutCalledCount);
-
- Thread.Sleep(2000); // Internally the keep alive timeout is multiplied with 1.5 as per protocol specification.
-
- Assert.AreEqual(1, timeoutCalledCount);
- }
-
- [TestMethod]
- public void KeepAlive_NoTimeout()
- {
- var timeoutCalledCount = 0;
-
- var monitor = new MqttClientKeepAliveMonitor(string.Empty, delegate
- {
- timeoutCalledCount++;
- return Task.FromResult(0);
- }, new MqttNetLogger());
-
- Assert.AreEqual(0, timeoutCalledCount);
-
- monitor.Start(1, CancellationToken.None);
-
- Assert.AreEqual(0, timeoutCalledCount);
-
- // Simulate traffic.
- Thread.Sleep(1000); // Internally the keep alive timeout is multiplied with 1.5 as per protocol specification.
- monitor.PacketReceived(new MqttPublishPacket());
- Thread.Sleep(1000);
- monitor.PacketReceived(new MqttPublishPacket());
- Thread.Sleep(1000);
-
- Assert.AreEqual(0, timeoutCalledCount);
-
- Thread.Sleep(2000);
-
- Assert.AreEqual(1, timeoutCalledCount);
- }
- }
- }
|