Skip to content

Commit

Permalink
Fix ConnectionManager tests (#3109)
Browse files Browse the repository at this point in the history
Fix tests, add more assert statement, and rename to use CloseCloudConnectionOnDeviceDisconnect as field name and envrionment key
  • Loading branch information
philipktlin authored Jun 16, 2020
1 parent fb33e62 commit 3a7df8d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public class ConnectionManager : IConnectionManager
readonly ICredentialsCache credentialsCache;
readonly IIdentityProvider identityProvider;
readonly IDeviceConnectivityManager connectivityManager;
readonly bool closeCloudConnectionWhenCloseDeviceConnection;
readonly bool closeCloudConnectionOnDeviceDisconnect;

public ConnectionManager(
ICloudConnectionProvider cloudConnectionProvider,
ICredentialsCache credentialsCache,
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));
Expand All @@ -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<IIdentity> CloudConnectionEstablished;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -254,7 +254,7 @@ await clientCredentials.ForEachAsync(
}
else
{
await this.RemoveDeviceConnection(device, this.closeCloudConnectionWhenCloseDeviceConnection);
await this.RemoveDeviceConnection(device, this.closeCloudConnectionOnDeviceDisconnect);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -181,7 +181,7 @@ void RegisterRoutingModule(
encryptTwinStore,
configUpdateFrequency,
experimentalFeatures,
closeCloudConnectionWhenCloseDeviceConnection));
closeCloudConnectionOnDeviceDisconnect));
}

void RegisterCommonModule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -259,7 +259,7 @@ protected override void Load(ContainerBuilder builder)
identityProvider,
deviceConnectivityManager,
this.maxConnectedClients,
this.closeCloudConnectionWhenCloseDeviceConnection);
this.closeCloudConnectionOnDeviceDisconnect);
return connectionManager;
})
.As<Task<IConnectionManager>>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -981,7 +984,7 @@ public async Task TestCloseDeviceAndCloudConnection(bool closeCloudConnectionWhe
credentialsCache,
GetIdentityProvider(),
deviceConnectivityManager,
closeCloudConnectionWhenCloseDeviceConnection: closeCloudConnectionWhenCloseDeviceConnection);
closeCloudConnectionOnDeviceDisconnect: closeCloudConnectionOnDeviceDisconnect);
await connectionManager.AddDeviceConnection(module1Identity, moduleProxy1);

// Act
Expand All @@ -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);
Expand Down

0 comments on commit 3a7df8d

Please sign in to comment.