Skip to content

Commit

Permalink
fix(e2e-tests): Updates tests to implement/ suppress warnings for IDi…
Browse files Browse the repository at this point in the history
…sposable resource disposal
  • Loading branch information
abhipsaMisra authored May 26, 2021
1 parent 3b65b45 commit 08593d6
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions e2e/test/iothub/AuthenticationWithTokenRefreshDisposalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,17 @@ private async Task ReuseAuthenticationMethod_SingleDevice(Client.TransportType t

// Create an instance of the device client, send a test message and then close and dispose it.
DeviceClient deviceClient = DeviceClient.Create(testDevice.IoTHubHostName, authenticationMethod, transport);
await deviceClient.SendEventAsync(new Client.Message()).ConfigureAwait(false);
using var message1 = new Client.Message();
await deviceClient.SendEventAsync(message1).ConfigureAwait(false);
await deviceClient.CloseAsync();
deviceClient.Dispose();
Logger.Trace("Test with instance 1 completed");

// Perform the same steps again, reusing the previously created authentication method instance to ensure
// that the sdk did not dispose the user supplied authentication method instance.
DeviceClient deviceClient2 = DeviceClient.Create(testDevice.IoTHubHostName, authenticationMethod, transport);
await deviceClient2.SendEventAsync(new Client.Message()).ConfigureAwait(false);
using var message2 = new Client.Message();
await deviceClient2.SendEventAsync(message2).ConfigureAwait(false);
await deviceClient2.CloseAsync();
deviceClient2.Dispose();
Logger.Trace("Test with instance 2 completed, reused the previously created authentication method instance for the device client.");
Expand Down Expand Up @@ -107,7 +109,9 @@ private async Task ReuseAuthenticationMethod_MuxedDevices(Client.TransportType t
for (int i = 0; i < devicesCount; i++)
{
TestDevice testDevice = await TestDevice.GetTestDeviceAsync(Logger, _devicePrefix).ConfigureAwait(false);
#pragma warning disable CA2000 // Dispose objects before losing scope - the authentication method is disposed at the end of the test.
var authenticationMethod = new DeviceAuthenticationSasToken(testDevice.ConnectionString);
#pragma warning restore CA2000 // Dispose objects before losing scope

testDevices.Add(testDevice);
authenticationMethods.Add(authenticationMethod);
Expand All @@ -116,7 +120,9 @@ private async Task ReuseAuthenticationMethod_MuxedDevices(Client.TransportType t
// Initialize the client instances, set the connection status change handler and open the connection.
for (int i = 0; i < devicesCount; i++)
{
#pragma warning disable CA2000 // Dispose objects before losing scope - the client instance is disposed during the course of the test.
DeviceClient deviceClient = DeviceClient.Create(testDevices[i].IoTHubHostName, authenticationMethods[i], new ITransportSettings[] { amqpTransportSettings });
#pragma warning restore CA2000 // Dispose objects before losing scope

var amqpConnectionStatusChange = new AmqpConnectionStatusChange(testDevices[i].Id, Logger);
deviceClient.SetConnectionStatusChangesHandler(amqpConnectionStatusChange.ConnectionStatusChangesHandler);
Expand Down Expand Up @@ -157,11 +163,13 @@ private async Task ReuseAuthenticationMethod_MuxedDevices(Client.TransportType t
notRecovered.Should().BeFalse();

// Send a message through the rest of the multiplexed client instances.
var message = new Client.Message();
for (int i = 1; i < devicesCount; i++)
{
await deviceClients[i].SendEventAsync(new Client.Message()).ConfigureAwait(false);
await deviceClients[i].SendEventAsync(message).ConfigureAwait(false);
Logger.Trace($"Test with client {i} completed.");
}
message.Dispose();

// Close and dispose all of the client instances.
for (int i = 1; i < devicesCount; i++)
Expand All @@ -176,7 +184,9 @@ private async Task ReuseAuthenticationMethod_MuxedDevices(Client.TransportType t
// Initialize the client instances by reusing the created authentication methods and open the connection.
for (int i = 0; i < devicesCount; i++)
{
#pragma warning disable CA2000 // Dispose objects before losing scope - the client instance is disposed at the end of the test.
DeviceClient deviceClient = DeviceClient.Create(testDevices[i].IoTHubHostName, authenticationMethods[i], new ITransportSettings[] { amqpTransportSettings });
#pragma warning restore CA2000 // Dispose objects before losing scope

var amqpConnectionStatusChange = new AmqpConnectionStatusChange(testDevices[i].Id, Logger);
deviceClient.SetConnectionStatusChangesHandler(amqpConnectionStatusChange.ConnectionStatusChangesHandler);
Expand All @@ -187,12 +197,14 @@ private async Task ReuseAuthenticationMethod_MuxedDevices(Client.TransportType t
}

// Ensure that all clients are connected successfully, and the close and dispose the instances.
// Also dispose the authentication methods created.
for (int i = 0; i < devicesCount; i++)
{
amqpConnectionStatuses[i].LastConnectionStatus.Should().Be(ConnectionStatus.Connected);

await deviceClients[i].CloseAsync();
deviceClients[i].Dispose();
authenticationMethods[i].Dispose();

amqpConnectionStatuses[i].LastConnectionStatus.Should().Be(ConnectionStatus.Disabled);
}
Expand Down

0 comments on commit 08593d6

Please sign in to comment.