Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: Direct method subscription not removed after disconnect #3359

13 changes: 6 additions & 7 deletions iothub/device/src/Pipeline/MqttTransportHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,14 +1069,13 @@ private Task HandleDisconnectionAsync(MqttClientDisconnectedEventArgs disconnect
if (Logging.IsEnabled)
Logging.Info(this, $"MQTT connection was lost {disconnectedEventArgs.Exception}", nameof(HandleDisconnectionAsync));

if (disconnectedEventArgs.ClientWasConnected)
{
OnTransportDisconnected();
OnTransportDisconnected();

// During a disconnection, any pending twin updates won't be received, so we'll preemptively
// cancel these operations so the client can retry once reconnected.
RemoveOldOperations(TimeSpan.Zero);
}
// During a disconnection, any pending twin updates won't be received, so we'll preemptively
// cancel these operations so the client can retry once reconnected.
RemoveOldOperations(TimeSpan.Zero);

_mqttClient.ApplicationMessageReceivedAsync -= HandleReceivedMessageAsync;

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading;
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client.Transport;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -7,6 +8,8 @@

namespace Microsoft.Azure.Devices.Client.Tests.Transport.Mqtt
{
[TestClass]
[TestCategory("Unit")]
public class MqttTransportHandlerTests
{
[TestMethod]
Expand All @@ -24,6 +27,20 @@ public async Task MqttTransportHandler_OpenAsyncCallsConnectAsync()
mockMqttClient.Verify(p => p.ConnectAsync(It.IsAny<MqttClientOptions>(), cancellationToken));
}

[TestMethod]
public async Task mqtt()
{
var cancellationToken = new CancellationToken();
var options = new MqttClientOptions();

var mockMqttClient = new Mock<IMqttClient>();
using MqttTransportHandler mqttTransportHandler = CreateTransportHandler(mockMqttClient.Object);
await mqttTransportHandler.OpenAsync(cancellationToken);
mockMqttClient.VerifyAdd(p => p.ApplicationMessageReceivedAsync += It.IsAny<Func<MqttApplicationMessageReceivedEventArgs, Task>>());
mockMqttClient.Raise(p => p.DisconnectedAsync += null, It.IsAny<Func<MqttClientDisconnectedEventArgs, Task>>());
mockMqttClient.VerifyRemove(p => p.ApplicationMessageReceivedAsync -= It.IsAny<Func<MqttApplicationMessageReceivedEventArgs, Task>>());
}

internal static MqttTransportHandler CreateTransportHandler(IMqttClient mockMqttClient)
{
var pipelineContext = new PipelineContext();
Expand Down