Browse Source

Fix broken ClientWasConnected detection.

release/3.x.x
Christian Kratky 4 years ago
parent
commit
cf217034fa
3 changed files with 35 additions and 13 deletions
  1. +1
    -10
      Build/MQTTnet.nuspec
  2. +2
    -3
      Source/MQTTnet/Client/MqttClient.cs
  3. +32
    -0
      Tests/MQTTnet.Core.Tests/MqttClient_Tests.cs

+ 1
- 10
Build/MQTTnet.nuspec View File

@@ -12,16 +12,7 @@
<requireLicenseAcceptance>true</requireLicenseAcceptance>
<description>MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker) and supports v3.1.0, v3.1.1 and v5.0.0 of the MQTT protocol.</description>
<releaseNotes>
* [LowLevelClient] Fixed a null reference exception when connecting to a not existing server (thanks to @SGStino).
* [RcpClient] Adjusted some namespaces (BREAKING CHANGE!).
* [AspNetCore] Adjusted some namespaces (BREAKING CHANGE!).
* [Server] Adjusted some namespaces (BREAKING CHANGE!).
* [Server] Added state checks (throw if not started etc.) for most server APIs.
* [Server] Exposed real X509Certificate2 (instead byte array) to TLS options (thanks to @borigas).
* [Server] Fixed memory leak with TCP sockets (MqttServer is now Disposable!) (BREAKING CHANGE!).
* [Core] Fixed a null reference exception in the MqttTcpChannel with WriteAsync and ReadAsync.
* [Core] Added server interceptor for undelivered messages (thanks to @cshark-inator).
* [nuget] Added support for SourceLink (thanks to @JTOne123).
* [Client] Fixed wrong value for "ClientWasConnected" in "MqttClientDisconnectedEventArgs" #976 (thanks to @dbeinder).
</releaseNotes>
<copyright>Copyright Christian Kratky 2016-2020</copyright>
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin Blazor</tags>


+ 2
- 3
Source/MQTTnet/Client/MqttClient.cs View File

@@ -254,8 +254,7 @@ namespace MQTTnet.Client

_adapter?.Dispose();
}


protected override void Dispose(bool disposing)
{
if (disposing)
@@ -302,7 +301,7 @@ namespace MQTTnet.Client

async Task DisconnectInternalAsync(Task sender, Exception exception, MqttClientAuthenticateResult authenticateResult)
{
var clientWasConnected = IsConnected;
var clientWasConnected = _isConnected;
var reasonCode = MqttClientDisconnectReason.NormalDisconnection;

TryInitiateDisconnect();


+ 32
- 0
Tests/MQTTnet.Core.Tests/MqttClient_Tests.cs View File

@@ -23,6 +23,38 @@ namespace MQTTnet.Tests
{
public TestContext TestContext { get; set; }

[TestMethod]
public async Task Set_ClientWasConnected_On_ServerDisconnect()
{
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync();
var client = await testEnvironment.ConnectClientAsync();

Assert.IsTrue(client.IsConnected);
client.UseDisconnectedHandler(e => Assert.IsTrue(e.ClientWasConnected));

await server.StopAsync();
await Task.Delay(4000);
}
}

[TestMethod]
public async Task Set_ClientWasConnected_On_ClientDisconnect()
{
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync();
var client = await testEnvironment.ConnectClientAsync();

Assert.IsTrue(client.IsConnected);
client.UseDisconnectedHandler(e => Assert.IsTrue(e.ClientWasConnected));

await client.DisconnectAsync();
await Task.Delay(200);
}
}

[TestMethod]
[ExpectedException(typeof(MqttCommunicationTimedOutException))]
public async Task Connect_To_Invalid_Server_Wrong_IP()


Loading…
Cancel
Save