diff --git a/Source/MQTTnet/Client/Options/IMqttClientOptions.cs b/Source/MQTTnet/Client/Options/IMqttClientOptions.cs
index 5f1dd437..01e64f0f 100644
--- a/Source/MQTTnet/Client/Options/IMqttClientOptions.cs
+++ b/Source/MQTTnet/Client/Options/IMqttClientOptions.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using MQTTnet.Diagnostics;
using MQTTnet.Formatter;
@@ -91,7 +91,7 @@ namespace MQTTnet.Client
/// Gets the session expiry interval.
/// The time after a session expires when it's not actively used.
///
- uint? SessionExpiryInterval { get; }
+ uint SessionExpiryInterval { get; }
///
/// Gets the topic alias maximum.
diff --git a/Source/MQTTnet/Client/Options/MqttClientOptions.cs b/Source/MQTTnet/Client/Options/MqttClientOptions.cs
index cff39899..2b388edf 100644
--- a/Source/MQTTnet/Client/Options/MqttClientOptions.cs
+++ b/Source/MQTTnet/Client/Options/MqttClientOptions.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using MQTTnet.Diagnostics;
using MQTTnet.Formatter;
@@ -91,7 +91,7 @@ namespace MQTTnet.Client
/// Gets or sets the session expiry interval.
/// The time after a session expires when it's not actively used.
///
- public uint? SessionExpiryInterval { get; set; }
+ public uint SessionExpiryInterval { get; set; }
///
/// Gets or sets the topic alias maximum.
diff --git a/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs b/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs
index 5977f274..3016cd51 100644
--- a/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs
+++ b/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -112,7 +112,7 @@ namespace MQTTnet.Client
return this;
}
- public MqttClientOptionsBuilder WithSessionExpiryInterval(uint? sessionExpiryInterval)
+ public MqttClientOptionsBuilder WithSessionExpiryInterval(uint sessionExpiryInterval)
{
_options.SessionExpiryInterval = sessionExpiryInterval;
return this;
diff --git a/Source/MQTTnet/Packets/MqttConnectPacketProperties.cs b/Source/MQTTnet/Packets/MqttConnectPacketProperties.cs
index cbf0cba3..a1187480 100644
--- a/Source/MQTTnet/Packets/MqttConnectPacketProperties.cs
+++ b/Source/MQTTnet/Packets/MqttConnectPacketProperties.cs
@@ -1,4 +1,4 @@
-using System.Collections.Generic;
+using System.Collections.Generic;
namespace MQTTnet.Packets
{
@@ -6,7 +6,7 @@ namespace MQTTnet.Packets
{
public uint? WillDelayInterval { get; set; }
- public uint? SessionExpiryInterval { get; set; }
+ public uint SessionExpiryInterval { get; set; }
public string AuthenticationMethod { get; set; }
diff --git a/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs b/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs
index 01fe66aa..f1912b1e 100644
--- a/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs
+++ b/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs
@@ -431,31 +431,6 @@ namespace MQTTnet.Server
sessionShouldPersist = !connectPacket.CleanSession;
}
- bool sessionShouldPersist;
-
- if (context.ProtocolVersion == MqttProtocolVersion.V500)
- {
- // MQTT 5.0 section 3.1.2.11.2
- // The Client and Server MUST store the Session State after the Network Connection is closed if the Session Expiry Interval is greater than 0 [MQTT-3.1.2-23].
- //
- // A Client that only wants to process messages while connected will set the Clean Start to 1 and set the Session Expiry Interval to 0.
- // It will not receive Application Messages published before it connected and has to subscribe afresh to any topics that it is interested
- // in each time it connects.
-
- // Persist if SessionExpiryInterval != 0, but may start with a clean session
- sessionShouldPersist = context.SessionExpiryInterval.GetValueOrDefault() != 0;
- }
- else
- {
- // MQTT 3.1.1 section 3.1.2.4: persist only if 'not CleanSession'
- //
- // If CleanSession is set to 1, the Client and Server MUST discard any previous Session and start a new one.
- // This Session lasts as long as the Network Connection. State data associated with this Session MUST NOT be
- // reused in any subsequent Session [MQTT-3.1.2-6].
-
- sessionShouldPersist = !connectPacket.CleanSession;
- }
-
using (await _createConnectionSyncRoot.WaitAsync(CancellationToken.None).ConfigureAwait(false))
{
MqttSession session;
diff --git a/Source/MQTTnet/Server/Internal/MqttSession.cs b/Source/MQTTnet/Server/Internal/MqttSession.cs
index 4e35680e..36d1d390 100644
--- a/Source/MQTTnet/Server/Internal/MqttSession.cs
+++ b/Source/MQTTnet/Server/Internal/MqttSession.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
@@ -43,7 +43,7 @@ namespace MQTTnet.Server
///
/// Session should persist if CleanSession was set to false (Mqtt3) or if SessionExpiryInterval != 0 (Mqtt5)
///
- public bool IsPersistent { get; }
+ public bool IsPersistent { get; set; }
public MqttPacketIdentifierProvider PacketIdentifierProvider { get; } = new MqttPacketIdentifierProvider();
diff --git a/Tests/MQTTnet.Core.Tests/MQTTv5/Server_Tests.cs b/Tests/MQTTnet.Core.Tests/MQTTv5/Server_Tests.cs
index 75b2087c..9d5e14d2 100644
--- a/Tests/MQTTnet.Core.Tests/MQTTv5/Server_Tests.cs
+++ b/Tests/MQTTnet.Core.Tests/MQTTv5/Server_Tests.cs
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Client;
using MQTTnet.Formatter;
+using MQTTnet.Tests.Mockups;
using System.Threading;
using System.Threading.Tasks;
@@ -80,7 +81,7 @@ namespace MQTTnet.Tests.MQTTv5
[TestMethod]
public async Task Connect_with_Undefined_SessionExpiryInterval()
{
- using (var testEnvironment = new TestEnvironment(TestContext))
+ using (var testEnvironment = CreateTestEnvironment())
{
// Create server with persistent sessions enabled
@@ -91,7 +92,7 @@ namespace MQTTnet.Tests.MQTTv5
// Create client without clean session and NO session expiry interval,
// that means, the session should not persist
- var options1 = CreateClientOptions(testEnvironment, ClientId, false, null);
+ var options1 = CreateClientOptions(testEnvironment, ClientId, false, 0);
var client1 = await testEnvironment.ConnectClient(options1);
// Disconnect; no session should remain on server because the session expiry interval was undefined
@@ -120,7 +121,7 @@ namespace MQTTnet.Tests.MQTTv5
[TestMethod]
public async Task Reconnect_with_different_SessionExpiryInterval()
{
- using (var testEnvironment = new TestEnvironment(TestContext))
+ using (var testEnvironment = CreateTestEnvironment())
{
// Create server with persistent sessions enabled
@@ -137,16 +138,13 @@ namespace MQTTnet.Tests.MQTTv5
await client1.DisconnectAsync();
- var result = await client2.ConnectAsync(options).ConfigureAwait(false);
- // Simulate some time delay between connections
-
await Task.Delay(1000);
// Reconnect the same client ID to the existing session but leave session expiry interval undefined this time.
// Session should be present because the client1 connection had SessionExpiryInterval > 0
var client2 = testEnvironment.CreateClient();
- var options2 = CreateClientOptions(testEnvironment, ClientId, false, null);
+ var options2 = CreateClientOptions(testEnvironment, ClientId, false, 0);
var result2 = await client2.ConnectAsync(options2).ConfigureAwait(false);
await client2.DisconnectAsync();
@@ -161,7 +159,7 @@ namespace MQTTnet.Tests.MQTTv5
// No session should be present because the previous session expiry interval was undefined for the client2 connection
var client3 = testEnvironment.CreateClient();
- var options3 = CreateClientOptions(testEnvironment, ClientId, false, null);
+ var options3 = CreateClientOptions(testEnvironment, ClientId, false, 0);
var result3 = await client2.ConnectAsync(options3).ConfigureAwait(false);
await client3.DisconnectAsync();
@@ -173,7 +171,7 @@ namespace MQTTnet.Tests.MQTTv5
}
}
- IMqttClientOptions CreateClientOptions(TestEnvironment testEnvironment, string clientId, bool cleanSession, uint? sessionExpiryInterval)
+ IMqttClientOptions CreateClientOptions(TestEnvironment testEnvironment, string clientId, bool cleanSession, uint sessionExpiryInterval)
{
return testEnvironment.Factory.CreateClientOptionsBuilder()
.WithProtocolVersion(MqttProtocolVersion.V500)
diff --git a/Tests/MQTTnet.Core.Tests/RPC_Tests.cs b/Tests/MQTTnet.Core.Tests/RPC_Tests.cs
index d4ecadf0..4efce244 100644
--- a/Tests/MQTTnet.Core.Tests/RPC_Tests.cs
+++ b/Tests/MQTTnet.Core.Tests/RPC_Tests.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -10,14 +10,13 @@ using MQTTnet.Protocol;
using MQTTnet.Formatter;
using MQTTnet.Extensions.Rpc.Options;
using MQTTnet.Extensions.Rpc.Options.TopicGeneration;
+using MQTTnet.Implementations;
namespace MQTTnet.Tests
{
[TestClass]
- public class RPC_Tests
+ public sealed class RPC_Tests : BaseTestClass
{
- public TestContext TestContext { get; set; }
-
[TestMethod]
public Task Execute_Success_With_QoS_0()
{
@@ -77,7 +76,7 @@ namespace MQTTnet.Tests
[ExpectedException(typeof(MqttCommunicationTimedOutException))]
public async Task Execute_Timeout()
{
- using (var testEnvironment = new TestEnvironment(TestContext))
+ using (var testEnvironment = CreateTestEnvironment())
{
await testEnvironment.StartServer();
@@ -92,7 +91,7 @@ namespace MQTTnet.Tests
[ExpectedException(typeof(MqttCommunicationTimedOutException))]
public async Task Execute_With_Custom_Topic_Names()
{
- using (var testEnvironment = new TestEnvironment(TestContext))
+ using (var testEnvironment = CreateTestEnvironment())
{
await testEnvironment.StartServer();
@@ -106,7 +105,7 @@ namespace MQTTnet.Tests
async Task Execute_Success(MqttQualityOfServiceLevel qosLevel, MqttProtocolVersion protocolVersion)
{
- using (var testEnvironment = new TestEnvironment(TestContext))
+ using (var testEnvironment = CreateTestEnvironment())
{
await testEnvironment.StartServer();
var responseSender = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithProtocolVersion(protocolVersion));
@@ -127,16 +126,16 @@ namespace MQTTnet.Tests
async Task Execute_Success_MQTT_V5(MqttQualityOfServiceLevel qosLevel)
{
- using (var testEnvironment = new TestEnvironment(TestContext))
+ using (var testEnvironment = CreateTestEnvironment())
{
await testEnvironment.StartServer();
var responseSender = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500));
await responseSender.SubscribeAsync("MQTTnet.RPC/+/ping", qosLevel);
- responseSender.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(async e =>
+ responseSender.ApplicationMessageReceivedAsync += async e =>
{
await responseSender.PublishAsync(e.ApplicationMessage.ResponseTopic, "pong");
- });
+ };
var requestSender = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500));
@@ -152,17 +151,17 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Execute_Success_MQTT_V5_Mixed_Clients()
{
- using (var testEnvironment = new TestEnvironment(TestContext))
+ using (var testEnvironment = CreateTestEnvironment())
{
await testEnvironment.StartServer();
var responseSender = await testEnvironment.ConnectClient();
await responseSender.SubscribeAsync("MQTTnet.RPC/+/ping", MqttQualityOfServiceLevel.AtMostOnce);
- responseSender.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(async e =>
+ responseSender.ApplicationMessageReceivedAsync += async e =>
{
Assert.IsNull(e.ApplicationMessage.ResponseTopic);
await responseSender.PublishAsync(e.ApplicationMessage.Topic + "/response", "pong");
- });
+ };
var requestSender = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500));
@@ -185,10 +184,11 @@ namespace MQTTnet.Tests
var responseSender = await testEnvironment.ConnectClient();
await responseSender.SubscribeAsync("MQTTnet.RPC/+/ping", MqttQualityOfServiceLevel.AtMostOnce);
- responseSender.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(async e =>
+ responseSender.ApplicationMessageReceivedAsync += async e =>
{
Assert.IsNull(e.ApplicationMessage.ResponseTopic);
- });
+ await PlatformAbstractionLayer.CompletedTask;
+ };
var requestSender = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500));
diff --git a/Tests/MQTTnet.Core.Tests/Server/Session_Tests.cs b/Tests/MQTTnet.Core.Tests/Server/Session_Tests.cs
index 5c21e0c2..47289947 100644
--- a/Tests/MQTTnet.Core.Tests/Server/Session_Tests.cs
+++ b/Tests/MQTTnet.Core.Tests/Server/Session_Tests.cs
@@ -263,58 +263,6 @@ 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 TryConnect(TestEnvironment testEnvironment, MqttClientOptionsBuilder options)
{
try