From 3a7df8d492acc44674e8cfcb36d2d7b2cd8284f5 Mon Sep 17 00:00:00 2001 From: Philip Lin Date: Tue, 16 Jun 2020 16:11:57 -0700 Subject: [PATCH] Fix ConnectionManager tests (#3109) Fix tests, add more assert statement, and rename to use CloseCloudConnectionOnDeviceDisconnect as field name and envrionment key --- .../ConnectionManager.cs | 10 +++++----- .../DependencyManager.cs | 4 ++-- .../modules/RoutingModule.cs | 8 ++++---- .../ConnectionManagerTest.cs | 13 ++++++++----- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/ConnectionManager.cs b/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/ConnectionManager.cs index d2a4f65557c..17df39256b5 100644 --- a/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/ConnectionManager.cs +++ b/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/ConnectionManager.cs @@ -29,7 +29,7 @@ public class ConnectionManager : IConnectionManager readonly ICredentialsCache credentialsCache; readonly IIdentityProvider identityProvider; readonly IDeviceConnectivityManager connectivityManager; - readonly bool closeCloudConnectionWhenCloseDeviceConnection; + readonly bool closeCloudConnectionOnDeviceDisconnect; public ConnectionManager( ICloudConnectionProvider cloudConnectionProvider, @@ -37,7 +37,7 @@ public ConnectionManager( IIdentityProvider identityProvider, IDeviceConnectivityManager connectivityManager, int maxClients = DefaultMaxClients, - bool closeCloudConnectionWhenCloseDeviceConnection = true) + bool closeCloudConnectionOnDeviceDisconnect = true) { this.cloudConnectionProvider = Preconditions.CheckNotNull(cloudConnectionProvider, nameof(cloudConnectionProvider)); this.maxClients = Preconditions.CheckRange(maxClients, 1, nameof(maxClients)); @@ -46,7 +46,7 @@ public ConnectionManager( this.connectivityManager = Preconditions.CheckNotNull(connectivityManager, nameof(connectivityManager)); this.connectivityManager.DeviceDisconnected += (o, args) => this.HandleDeviceCloudConnectionDisconnected(); Util.Metrics.MetricsV0.RegisterGaugeCallback(() => MetricsV0.SetConnectedClientCountGauge(this)); - this.closeCloudConnectionWhenCloseDeviceConnection = closeCloudConnectionWhenCloseDeviceConnection; + this.closeCloudConnectionOnDeviceDisconnect = closeCloudConnectionOnDeviceDisconnect; } public event EventHandler CloudConnectionEstablished; @@ -78,7 +78,7 @@ await currentDeviceConnection public Task RemoveDeviceConnection(string id) { return this.devices.TryGetValue(Preconditions.CheckNonWhiteSpace(id, nameof(id)), out ConnectedDevice device) - ? this.RemoveDeviceConnection(device, this.closeCloudConnectionWhenCloseDeviceConnection) + ? this.RemoveDeviceConnection(device, this.closeCloudConnectionOnDeviceDisconnect) : Task.CompletedTask; } @@ -254,7 +254,7 @@ await clientCredentials.ForEachAsync( } else { - await this.RemoveDeviceConnection(device, this.closeCloudConnectionWhenCloseDeviceConnection); + await this.RemoveDeviceConnection(device, this.closeCloudConnectionOnDeviceDisconnect); } }); } diff --git a/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Service/DependencyManager.cs b/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Service/DependencyManager.cs index 6150de72cbf..68c85585876 100644 --- a/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Service/DependencyManager.cs +++ b/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Service/DependencyManager.cs @@ -151,7 +151,7 @@ void RegisterRoutingModule( bool encryptTwinStore = this.configuration.GetValue("EncryptTwinStore", false); int configUpdateFrequencySecs = this.configuration.GetValue("ConfigRefreshFrequencySecs", 3600); TimeSpan configUpdateFrequency = TimeSpan.FromSeconds(configUpdateFrequencySecs); - bool closeCloudConnectionWhenCloseDeviceConnection = this.configuration.GetValue("CloseCloudConnectionWhenCloseDeviceConnection", true); + bool closeCloudConnectionOnDeviceDisconnect = this.configuration.GetValue("CloseCloudConnectionOnDeviceDisconnect", true); builder.RegisterModule( new RoutingModule( @@ -181,7 +181,7 @@ void RegisterRoutingModule( encryptTwinStore, configUpdateFrequency, experimentalFeatures, - closeCloudConnectionWhenCloseDeviceConnection)); + closeCloudConnectionOnDeviceDisconnect)); } void RegisterCommonModule( diff --git a/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Service/modules/RoutingModule.cs b/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Service/modules/RoutingModule.cs index b99b77d7b19..e3716e367d0 100644 --- a/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Service/modules/RoutingModule.cs +++ b/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Service/modules/RoutingModule.cs @@ -53,7 +53,7 @@ public class RoutingModule : Module readonly bool encryptTwinStore; readonly TimeSpan configUpdateFrequency; readonly ExperimentalFeatures experimentalFeatures; - readonly bool closeCloudConnectionWhenCloseDeviceConnection; + readonly bool closeCloudConnectionOnDeviceDisconnect; public RoutingModule( string iotHubName, @@ -82,7 +82,7 @@ public RoutingModule( bool encryptTwinStore, TimeSpan configUpdateFrequency, ExperimentalFeatures experimentalFeatures, - bool closeCloudConnectionWhenCloseDeviceConnection) + bool closeCloudConnectionOnDeviceDisconnect) { this.iotHubName = Preconditions.CheckNonWhiteSpace(iotHubName, nameof(iotHubName)); this.edgeDeviceId = Preconditions.CheckNonWhiteSpace(edgeDeviceId, nameof(edgeDeviceId)); @@ -110,7 +110,7 @@ public RoutingModule( this.encryptTwinStore = encryptTwinStore; this.configUpdateFrequency = configUpdateFrequency; this.experimentalFeatures = experimentalFeatures; - this.closeCloudConnectionWhenCloseDeviceConnection = closeCloudConnectionWhenCloseDeviceConnection; + this.closeCloudConnectionOnDeviceDisconnect = closeCloudConnectionOnDeviceDisconnect; } protected override void Load(ContainerBuilder builder) @@ -259,7 +259,7 @@ protected override void Load(ContainerBuilder builder) identityProvider, deviceConnectivityManager, this.maxConnectedClients, - this.closeCloudConnectionWhenCloseDeviceConnection); + this.closeCloudConnectionOnDeviceDisconnect); return connectionManager; }) .As>() diff --git a/edge-hub/test/Microsoft.Azure.Devices.Edge.Hub.Core.Test/ConnectionManagerTest.cs b/edge-hub/test/Microsoft.Azure.Devices.Edge.Hub.Core.Test/ConnectionManagerTest.cs index 12f2fbed6e1..f251920da07 100644 --- a/edge-hub/test/Microsoft.Azure.Devices.Edge.Hub.Core.Test/ConnectionManagerTest.cs +++ b/edge-hub/test/Microsoft.Azure.Devices.Edge.Hub.Core.Test/ConnectionManagerTest.cs @@ -132,7 +132,11 @@ public async Task CloudConnectionTest() await connectionManager.RemoveDeviceConnection(deviceCredentials2.Identity.Id); returnedValue = await connectionManager.GetCloudConnection(deviceCredentials2.Identity.Id); + Assert.False(returnedValue.HasValue); + + returnedValue = await connectionManager.GetCloudConnection(deviceCredentials1.Identity.Id); Assert.True(returnedValue.HasValue); + Assert.True(returnedValue.OrDefault().IsActive); } [Fact] @@ -291,8 +295,7 @@ public async Task TestAddRemoveDeviceConnectionTest() Assert.False(deviceProxyMock1.Object.IsActive); cloudProxy = await connectionManager.GetCloudConnection(deviceId); - Assert.True(cloudProxy.HasValue); - Assert.True(client.IsActive); + Assert.False(cloudProxy.HasValue); } [Fact] @@ -939,7 +942,7 @@ public async Task GetMultipleCloudProxiesTest() [Theory] [InlineData(true)] [InlineData(false)] - public async Task TestCloseDeviceAndCloudConnection(bool closeCloudConnectionWhenCloseDeviceConnection) + public async Task TestCloseDeviceAndCloudConnection(bool closeCloudConnectionOnDeviceDisconnect) { // Arrange string edgeDeviceId = "edgeDevice"; @@ -981,7 +984,7 @@ public async Task TestCloseDeviceAndCloudConnection(bool closeCloudConnectionWhe credentialsCache, GetIdentityProvider(), deviceConnectivityManager, - closeCloudConnectionWhenCloseDeviceConnection: closeCloudConnectionWhenCloseDeviceConnection); + closeCloudConnectionOnDeviceDisconnect: closeCloudConnectionOnDeviceDisconnect); await connectionManager.AddDeviceConnection(module1Identity, moduleProxy1); // Act @@ -995,7 +998,7 @@ public async Task TestCloseDeviceAndCloudConnection(bool closeCloudConnectionWhe await connectionManager.RemoveDeviceConnection(module1Credentials.Identity.Id); // Assert - if (closeCloudConnectionWhenCloseDeviceConnection) + if (closeCloudConnectionOnDeviceDisconnect) { Assert.False(getCloudProxyTask.OrDefault().IsActive); Mock.Get(client1).Verify(cp => cp.CloseAsync(), Times.Once);