Squashed commit of the following:

commit 2d94abdde3
Author: Christian <christian.kratky@googlemail.com>
Date:   Sat Nov 13 10:09:59 2021 +0100

    Update MQTTnet.nuspec

    Update docs.

commit f0ea13c07f
Author: logicaloud <47704763+logicaloud@users.noreply.github.com>
Date:   Sat Nov 13 22:05:54 2021 +1300

    Fix MQTT 3.1.1 session persistence when CleanSession=1 (#1294)

commit 47cd5aa3cd
Author: Volker Milbrandt <milbrandt@users.noreply.github.com>
Date:   Sat Nov 13 10:02:55 2021 +0100

    test ResultCode for invalid credentials (no userName but password) (#1270)

    fix order of parameters in exceptions AreEqual(expected, actual)

commit 258df6bb14
Author: HansM <HansM2013@gmx.de>
Date:   Thu Nov 11 20:10:35 2021 +0100

    Removed IDisposable from MqttClientConnection class. (#1288)

commit 85c3c0b352
Author: SeppPenner <HansM2013@gmx.de>
Date:   Mon Nov 8 08:50:59 2021 +0100

    MQTTnet.noUWP.sln: Removed no longer present MQTTnet.Server project.
This commit is contained in:
Christian
2021-11-13 10:24:46 +01:00
parent 7993db2d82
commit b5b713570f
9 changed files with 106 additions and 48 deletions
+2 -2
View File
@@ -1021,7 +1021,7 @@ namespace MQTTnet.Tests.Server
var server = await testEnvironment.StartServer(new MqttServerOptionsBuilder().WithPersistentSessions());
// Create the session including the subscription.
var client1 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithClientId("a"));
var client1 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithClientId("a").WithCleanSession(false));
await client1.SubscribeAsync("x");
await client1.DisconnectAsync();
await Task.Delay(500);
@@ -1029,7 +1029,7 @@ namespace MQTTnet.Tests.Server
var clientStatus = await server.GetClientsAsync();
Assert.AreEqual(0, clientStatus.Count);
var client2 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithClientId("b"));
var client2 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithClientId("b").WithCleanSession(false));
await client2.PublishAsync("x", "1");
await client2.PublishAsync("x", "2");
await client2.PublishAsync("x", "3");
@@ -1,4 +1,4 @@
using System.Threading.Tasks;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Adapter;
using MQTTnet.Client;
@@ -32,6 +32,7 @@ namespace MQTTnet.Tests.Server
var ex = await Assert.ThrowsExceptionAsync<MqttConnectingFailedException>(async () => await client.ConnectAsync(clientOptions));
Assert.IsInstanceOfType(ex.InnerException, typeof(MqttProtocolViolationException));
Assert.AreEqual("Error while authenticating. If the User Name Flag is set to 0, the Password Flag MUST be set to 0 [MQTT-3.1.2-22].", ex.Message, false);
Assert.AreEqual(MqttClientConnectResultCode.UnspecifiedError, ex.ResultCode);
}
}
@@ -53,7 +54,7 @@ namespace MQTTnet.Tests.Server
return TestCredentials("x", "x");
}
async Task TestCredentials(string userName, string password)
private async Task TestCredentials(string userName, string password)
{
using (var testEnvironment = CreateTestEnvironment())
{
@@ -84,7 +85,7 @@ namespace MQTTnet.Tests.Server
.Build();
var ex = await Assert.ThrowsExceptionAsync<MqttConnectingFailedException>(async () => await client.ConnectAsync(clientOptions));
Assert.AreEqual(ex.Result.ResultCode, MqttClientConnectResultCode.BadUserNameOrPassword);
Assert.AreEqual(MqttClientConnectResultCode.BadUserNameOrPassword, ex.Result.ResultCode);
}
}
}
@@ -1,4 +1,3 @@
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -213,6 +212,58 @@ namespace MQTTnet.Tests.Server
}
}
[TestMethod]
public async Task Clean_Session_Persistence()
{
using (var testEnvironment = new TestEnvironment(TestContext))
{
// Create server with persistent sessions enabled
await testEnvironment.StartServer(o => o.WithPersistentSessions());
const string ClientId = "Client1";
// Create client with clean session and long session expiry interval
var client1 = await testEnvironment.ConnectClient(o => o
.WithProtocolVersion(Formatter.MqttProtocolVersion.V311)
.WithTcpServer("127.0.0.1", testEnvironment.ServerPort)
.WithSessionExpiryInterval(9999) // not relevant for v311 but testing impact
.WithCleanSession(true) // start and end with clean session
.WithClientId(ClientId)
.Build()
);
// Disconnect; empty session should be removed from server
await client1.DisconnectAsync();
// Simulate some time delay between connections
await Task.Delay(1000);
// Reconnect the same client ID without clean session
var client2 = testEnvironment.CreateClient();
var options = testEnvironment.Factory.CreateClientOptionsBuilder()
.WithProtocolVersion(Formatter.MqttProtocolVersion.V311)
.WithTcpServer("127.0.0.1", testEnvironment.ServerPort)
.WithSessionExpiryInterval(9999) // not relevant for v311 but testing impact
.WithCleanSession(false) // see if there is a session
.WithClientId(ClientId)
.Build();
var result = await client2.ConnectAsync(options).ConfigureAwait(false);
await client2.DisconnectAsync();
// Session should NOT be present for MQTT v311 and initial CleanSession == true
Assert.IsTrue(!result.IsSessionPresent, "Session present");
}
}
async Task<IMqttClient> TryConnect(TestEnvironment testEnvironment, MqttClientOptionsBuilder options)
{
try
@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Client;
@@ -80,8 +80,8 @@ namespace MQTTnet.Tests.Server
{
var server = await testEnvironment.StartServer(new MqttServerOptionsBuilder().WithPersistentSessions());
var c1 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithClientId("client1"));
var c2 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithClientId("client2"));
var c1 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithClientId("client1").WithCleanSession(false));
var c2 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithClientId("client2").WithCleanSession(false));
await c1.DisconnectAsync();