Skip to content

Commit

Permalink
Fix edge agent connection (#3172) (#3173)
Browse files Browse the repository at this point in the history
* Recreate connection after every exception
  • Loading branch information
ancaantochi authored Jul 13, 2020
1 parent 0569489 commit 23ffb26
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ async Task<Twin> GetTwinFunc()
{
Events.ErrorGettingTwin(e);

if (!retrying && moduleClient != null && !(e is TimeoutException))
if (!retrying && moduleClient != null)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.Azure.Devices.Edge.Agent.IoTHub.SdkClient
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client;
Expand All @@ -14,7 +15,18 @@ public class WrappingSdkModuleClient : ISdkModuleClient
public WrappingSdkModuleClient(ModuleClient sdkModuleClient)
=> this.sdkModuleClient = Preconditions.CheckNotNull(sdkModuleClient, nameof(sdkModuleClient));

public Task OpenAsync() => this.sdkModuleClient.OpenAsync();
public Task OpenAsync()
{
try
{
return this.sdkModuleClient.OpenAsync();
}
catch (Exception)
{
this.sdkModuleClient?.Dispose();
throw;
}
}

public void SetConnectionStatusChangesHandler(ConnectionStatusChangesHandler statusChangesHandler)
=> this.sdkModuleClient.SetConnectionStatusChangesHandler(statusChangesHandler);
Expand Down Expand Up @@ -49,6 +61,10 @@ public Task UpdateReportedPropertiesAsync(TwinCollection reportedProperties)
//// return await EdgeClientWebSocket.Connect(deviceStreamRequest.Url, deviceStreamRequest.AuthorizationToken, cancellationToken);
////}

public Task CloseAsync() => this.sdkModuleClient.CloseAsync();
public Task CloseAsync()
{
this.sdkModuleClient.Dispose();
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1525,9 +1525,11 @@ public async Task GetTwinRetryLogicGetsNewClient()
}
}

[Fact]
[Theory]
[Unit]
public async Task GetDeploymentConfigInfoAsync_CreateNewModuleClientWhenGetTwinThrowsMoreThanRetryCount()
[InlineData(typeof(InvalidOperationException))]
[InlineData(typeof(TimeoutException))]
public async Task GetDeploymentConfigInfoAsync_CreateNewModuleClientWhenGetTwinThrowsMoreThanRetryCount(Type thrownException)
{
// Arrange
var moduleClient = new Mock<IModuleClient>();
Expand Down Expand Up @@ -1580,9 +1582,10 @@ public async Task GetDeploymentConfigInfoAsync_CreateNewModuleClientWhenGetTwinT
}
};

var ex = Activator.CreateInstance(thrownException, "msg str") as Exception;
moduleClient.SetupSequence(d => d.GetTwinAsync())
.ThrowsAsync(new InvalidOperationException())
.ThrowsAsync(new InvalidOperationException())
.ThrowsAsync(ex)
.ThrowsAsync(ex)
.ReturnsAsync(twin);
moduleClient.Setup(d => d.SetDesiredPropertyUpdateCallbackAsync(It.IsAny<DesiredPropertyUpdateCallback>()))
.Returns(Task.CompletedTask);
Expand Down

0 comments on commit 23ffb26

Please sign in to comment.