|
|
@@ -16,31 +16,43 @@ namespace MQTTnet.Tests |
|
|
|
[TestMethod] |
|
|
|
public void KeepAlive_Timeout() |
|
|
|
{ |
|
|
|
var clientSession = new TestClientSession(); |
|
|
|
var monitor = new MqttClientKeepAliveMonitor(clientSession, new MqttNetLogger().CreateChildLogger()); |
|
|
|
var counter = 0; |
|
|
|
|
|
|
|
Assert.AreEqual(0, clientSession.StopCalledCount); |
|
|
|
var monitor = new MqttClientKeepAliveMonitor("", () => |
|
|
|
{ |
|
|
|
counter++; |
|
|
|
return Task.CompletedTask; |
|
|
|
}, |
|
|
|
new MqttNetLogger().CreateChildLogger()); |
|
|
|
|
|
|
|
Assert.AreEqual(0, counter); |
|
|
|
|
|
|
|
monitor.Start(1, CancellationToken.None); |
|
|
|
|
|
|
|
Assert.AreEqual(0, clientSession.StopCalledCount); |
|
|
|
Assert.AreEqual(0, counter); |
|
|
|
|
|
|
|
Thread.Sleep(2000); // Internally the keep alive timeout is multiplied with 1.5 as per protocol specification. |
|
|
|
|
|
|
|
Assert.AreEqual(1, clientSession.StopCalledCount); |
|
|
|
Assert.AreEqual(1, counter); |
|
|
|
} |
|
|
|
|
|
|
|
[TestMethod] |
|
|
|
public void KeepAlive_NoTimeout() |
|
|
|
{ |
|
|
|
var clientSession = new TestClientSession(); |
|
|
|
var monitor = new MqttClientKeepAliveMonitor(clientSession, new MqttNetLogger().CreateChildLogger()); |
|
|
|
var counter = 0; |
|
|
|
|
|
|
|
var monitor = new MqttClientKeepAliveMonitor("", () => |
|
|
|
{ |
|
|
|
counter++; |
|
|
|
return Task.CompletedTask; |
|
|
|
}, |
|
|
|
new MqttNetLogger().CreateChildLogger()); |
|
|
|
|
|
|
|
Assert.AreEqual(0, clientSession.StopCalledCount); |
|
|
|
Assert.AreEqual(0, counter); |
|
|
|
|
|
|
|
monitor.Start(1, CancellationToken.None); |
|
|
|
|
|
|
|
Assert.AreEqual(0, clientSession.StopCalledCount); |
|
|
|
Assert.AreEqual(0, counter); |
|
|
|
|
|
|
|
// Simulate traffic. |
|
|
|
Thread.Sleep(1000); // Internally the keep alive timeout is multiplied with 1.5 as per protocol specification. |
|
|
@@ -49,11 +61,11 @@ namespace MQTTnet.Tests |
|
|
|
monitor.PacketReceived(); |
|
|
|
Thread.Sleep(1000); |
|
|
|
|
|
|
|
Assert.AreEqual(0, clientSession.StopCalledCount); |
|
|
|
Assert.AreEqual(0, counter); |
|
|
|
|
|
|
|
Thread.Sleep(2000); |
|
|
|
|
|
|
|
Assert.AreEqual(1, clientSession.StopCalledCount); |
|
|
|
Assert.AreEqual(1, counter); |
|
|
|
} |
|
|
|
|
|
|
|
private class TestClientSession : IMqttClientSession |
|
|
|