Use latest language features (#2176)

* Make use of language features line AsyncDispose, init etc.

* Fix build errors

* Improve Unit Tests

* Improve Unit Tests

* Improve Unit Tests

* Update samples

* Remove obsolete benchmarks

* Code cleanup

* Improve unit tests

* Improve Unit Tests

* Fix Unit Tests

* Improve Unit Tests

* Improve Unit Tests

* Restore unit tests

* Exclude load tests from unit tests
This commit is contained in:
Christian
2025-08-30 10:52:59 +02:00
committed by GitHub
parent 247988e633
commit e43e7f8e36
330 changed files with 21417 additions and 22919 deletions
+45 -47
View File
@@ -10,59 +10,57 @@ using MQTTnet.Formatter;
using MQTTnet.Packets;
using MQTTnet.Protocol;
namespace MQTTnet.Tests.Server
namespace MQTTnet.Tests.Server;
// ReSharper disable InconsistentNaming
[TestClass]
public sealed class KeepAlive_Tests : BaseTestClass
{
[TestClass]
public sealed class KeepAlive_Tests : BaseTestClass
[TestMethod]
public async Task Disconnect_Client_DueTo_KeepAlive()
{
[TestMethod]
public async Task Disconnect_Client_DueTo_KeepAlive()
using var testEnvironment = CreateTestEnvironment();
await testEnvironment.StartServer();
var client = await testEnvironment.ConnectLowLevelClient(o => o
.WithTimeout(TimeSpan.FromSeconds(1))
.WithTimeout(TimeSpan.Zero)
.WithProtocolVersion(MqttProtocolVersion.V500)).ConfigureAwait(false);
await client.SendAsync(new MqttConnectPacket
{
using (var testEnvironment = CreateTestEnvironment())
{
await testEnvironment.StartServer();
CleanSession = true,
ClientId = "Disconnect_Client_DueTo_KeepAlive",
KeepAlivePeriod = 1
}, CancellationToken.None).ConfigureAwait(false);
var client = await testEnvironment.ConnectLowLevelClient(o => o
.WithTimeout(TimeSpan.FromSeconds(1))
.WithTimeout(TimeSpan.Zero)
.WithProtocolVersion(MqttProtocolVersion.V500)).ConfigureAwait(false);
var responsePacket = await client.ReceiveAsync(CancellationToken.None).ConfigureAwait(false);
Assert.IsTrue(responsePacket is MqttConnAckPacket);
await client.SendAsync(new MqttConnectPacket
{
CleanSession = true,
ClientId = "Disconnect_Client_DueTo_KeepAlive",
KeepAlivePeriod = 1
}, CancellationToken.None).ConfigureAwait(false);
for (var i = 0; i < 6; i++)
{
await Task.Delay(500);
var responsePacket = await client.ReceiveAsync(CancellationToken.None).ConfigureAwait(false);
Assert.IsTrue(responsePacket is MqttConnAckPacket);
for (var i = 0; i < 6; i++)
{
await Task.Delay(500);
await client.SendAsync(MqttPingReqPacket.Instance, CancellationToken.None);
responsePacket = await client.ReceiveAsync(CancellationToken.None);
Assert.IsTrue(responsePacket is MqttPingRespPacket);
}
// If we reach this point everything works as expected (server did not close the connection
// due to proper ping messages.
// Now we will wait 1.1 seconds because the server MUST wait 1.5 seconds in total (See spec).
await Task.Delay(1100);
await client.SendAsync(MqttPingReqPacket.Instance, CancellationToken.None);
responsePacket = await client.ReceiveAsync(CancellationToken.None);
Assert.IsTrue(responsePacket is MqttPingRespPacket);
// Now we will wait longer than 1.5 so that the server will close the connection.
responsePacket = await client.ReceiveAsync(CancellationToken.None);
var disconnectPacket = responsePacket as MqttDisconnectPacket;
Assert.IsTrue(disconnectPacket != null);
Assert.AreEqual(disconnectPacket.ReasonCode, MqttDisconnectReasonCode.KeepAliveTimeout);
}
await client.SendAsync(MqttPingReqPacket.Instance, CancellationToken.None);
responsePacket = await client.ReceiveAsync(CancellationToken.None);
Assert.IsTrue(responsePacket is MqttPingRespPacket);
}
// If we reach this point everything works as expected (server did not close the connection
// due to proper ping messages.
// Now we will wait 1.1 seconds because the server MUST wait 1.5 seconds in total (See spec).
await Task.Delay(1100);
await client.SendAsync(MqttPingReqPacket.Instance, CancellationToken.None);
responsePacket = await client.ReceiveAsync(CancellationToken.None);
Assert.IsTrue(responsePacket is MqttPingRespPacket);
// Now we will wait longer than 1.5 so that the server will close the connection.
responsePacket = await client.ReceiveAsync(CancellationToken.None);
var disconnectPacket = responsePacket as MqttDisconnectPacket;
Assert.IsTrue(disconnectPacket != null);
Assert.AreEqual(disconnectPacket.ReasonCode, MqttDisconnectReasonCode.KeepAliveTimeout);
}
}