Files
MQTTnet/Tests/MQTTnet.Core.Tests/MqttKeepAliveMonitorTests.cs
T

67 lines
1.9 KiB
C#

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);
}
}
}