From c90cd00cddc2bdc6d612ec961051743bb22d6e02 Mon Sep 17 00:00:00 2001 From: "David R. Williamson" Date: Tue, 18 Feb 2020 14:37:03 -0800 Subject: [PATCH] Test code cleanup (#1219) Co-authored-by: Abhipsa Misra --- CodeMaid.config | 3 + common/test/Configuration.cs | 2 +- ...rityCenterForIoTSecurityMessageE2ETests.cs | 74 ++++++++++++------- e2e/test/FileUploadFaultInjectionTests.cs | 74 ++++++++++--------- 4 files changed, 90 insertions(+), 63 deletions(-) diff --git a/CodeMaid.config b/CodeMaid.config index a793664609..740b9695a2 100644 --- a/CodeMaid.config +++ b/CodeMaid.config @@ -29,6 +29,9 @@ True + + 1 + diff --git a/common/test/Configuration.cs b/common/test/Configuration.cs index 3e5578b59e..0ab888d432 100644 --- a/common/test/Configuration.cs +++ b/common/test/Configuration.cs @@ -8,7 +8,7 @@ namespace Microsoft.Azure.Devices.E2ETests { public static partial class Configuration { - private static string GetValue(string envName, string defaultValue=null) + private static string GetValue(string envName, string defaultValue = null) { string envValue = Environment.GetEnvironmentVariable(envName); diff --git a/e2e/test/AzureSecurityCenterForIoTSecurityMessageE2ETests.cs b/e2e/test/AzureSecurityCenterForIoTSecurityMessageE2ETests.cs index fe734fadb0..ddf4754c75 100644 --- a/e2e/test/AzureSecurityCenterForIoTSecurityMessageE2ETests.cs +++ b/e2e/test/AzureSecurityCenterForIoTSecurityMessageE2ETests.cs @@ -19,10 +19,9 @@ public class AzureSecurityCenterForIoTSecurityMessageE2ETests : IDisposable { private readonly string _devicePrefix = $"E2E_{nameof(AzureSecurityCenterForIoTSecurityMessageE2ETests)}_"; private readonly string _modulePrefix = $"E2E_{nameof(AzureSecurityCenterForIoTSecurityMessageE2ETests)}_"; - private static TestLogging _log = TestLogging.GetInstance(); - private readonly ConsoleEventListener _listener; - private readonly AzureSecurityCenterForIoTLogAnalyticsClient _logAnalyticsClient; + private ConsoleEventListener _listener; + private AzureSecurityCenterForIoTLogAnalyticsClient _logAnalyticsClient; public AzureSecurityCenterForIoTSecurityMessageE2ETests() { @@ -33,55 +32,55 @@ public AzureSecurityCenterForIoTSecurityMessageE2ETests() [TestMethod] public Task SecurityMessage_DeviceSendSingleMessage_Amqp() { - return TestSecurityMessage(Client.TransportType.Amqp_Tcp_Only); + return TestSecurityMessageAsync(Client.TransportType.Amqp_Tcp_Only); } [TestMethod] public Task SecurityMessage_ModuleSendSingleMessage_Amqp() { - return TestSecurityMessageModule(Client.TransportType.Amqp_Tcp_Only); + return TestSecurityMessageModuleAsync(Client.TransportType.Amqp_Tcp_Only); } [TestMethod] public Task SecurityMessage_DeviceSendSingleMessage_AmqpWs() { - return TestSecurityMessage(Client.TransportType.Amqp_WebSocket_Only); + return TestSecurityMessageAsync(Client.TransportType.Amqp_WebSocket_Only); } [TestMethod] public Task SecurityMessage_ModuleSendSingleMessage_AmqpWs() { - return TestSecurityMessageModule(Client.TransportType.Amqp_WebSocket_Only); + return TestSecurityMessageModuleAsync(Client.TransportType.Amqp_WebSocket_Only); } [TestMethod] public Task SecurityMessage_DeviceSendSingleMessage_Mqtt() { - return TestSecurityMessage(Client.TransportType.Mqtt_Tcp_Only); + return TestSecurityMessageAsync(Client.TransportType.Mqtt_Tcp_Only); } [TestMethod] public Task SecurityMessage_ModuleSendSingleMessage_Mqtt() { - return TestSecurityMessageModule(Client.TransportType.Mqtt_Tcp_Only); + return TestSecurityMessageModuleAsync(Client.TransportType.Mqtt_Tcp_Only); } [TestMethod] public Task SecurityMessage_DeviceSendSingleMessage_MqttWs() { - return TestSecurityMessage(Client.TransportType.Mqtt_WebSocket_Only); + return TestSecurityMessageAsync(Client.TransportType.Mqtt_WebSocket_Only); } [TestMethod] public Task SecurityMessage_ModuleSendSingleMessage_MqttWs() { - return TestSecurityMessageModule(Client.TransportType.Mqtt_WebSocket_Only); + return TestSecurityMessageModuleAsync(Client.TransportType.Mqtt_WebSocket_Only); } [TestMethod] public Task SecurityMessage_DeviceSendSingleMessage_Http() { - return TestSecurityMessage(Client.TransportType.Http1); + return TestSecurityMessageAsync(Client.TransportType.Http1); } private Client.Message ComposeD2CSecurityTestMessage(out string eventId, out string payload, out string p1Value) @@ -89,7 +88,7 @@ private Client.Message ComposeD2CSecurityTestMessage(out string eventId, out str eventId = p1Value = Guid.NewGuid().ToString(); payload = ComposeAzureSecurityCenterForIoTSecurityMessagePayload(eventId).ToString(Newtonsoft.Json.Formatting.None); - Client.Message message = new Client.Message(Encoding.UTF8.GetBytes(payload)) + var message = new Client.Message(Encoding.UTF8.GetBytes(payload)) { Properties = { ["property1"] = p1Value } }; @@ -107,7 +106,8 @@ private JObject ComposeAzureSecurityCenterForIoTSecurityMessagePayload(string ev { "AgentId" , Guid.NewGuid().ToString() }, { "MessageSchemaVersion", "1.0" }, { "Events", new JArray - { new JObject + { + new JObject { { "EventType", "Security" }, { "Category", "Periodic" }, @@ -124,7 +124,7 @@ private JObject ComposeAzureSecurityCenterForIoTSecurityMessagePayload(string ev }; } - private async Task TestSecurityMessage(Client.TransportType transport) + private async Task TestSecurityMessageAsync(Client.TransportType transport) { TestDevice testDevice = await TestDevice.GetTestDeviceAsync(_devicePrefix).ConfigureAwait(false); @@ -132,7 +132,7 @@ private async Task TestSecurityMessage(Client.TransportType transport) { try { - await SendSingleSecurityMessage(deviceClient, testDevice.Id, _logAnalyticsClient).ConfigureAwait(false); + await SendSingleSecurityMessageAsync(deviceClient, testDevice.Id, _logAnalyticsClient).ConfigureAwait(false); } finally { @@ -141,15 +141,15 @@ private async Task TestSecurityMessage(Client.TransportType transport) } } - private async Task TestSecurityMessageModule(Client.TransportType transport) + private async Task TestSecurityMessageModuleAsync(Client.TransportType transport) { TestModule testModule = await TestModule.GetTestModuleAsync(_devicePrefix, _modulePrefix).ConfigureAwait(false); - using (ModuleClient moduleClient = ModuleClient.CreateFromConnectionString(testModule.ConnectionString, transport)) + using (var moduleClient = ModuleClient.CreateFromConnectionString(testModule.ConnectionString, transport)) { try { - await SendSingleSecurityMessageModule(moduleClient, testModule.DeviceId, _logAnalyticsClient).ConfigureAwait(false); + await SendSingleSecurityMessageModuleAsync(moduleClient, testModule.DeviceId, _logAnalyticsClient).ConfigureAwait(false); } finally { @@ -158,27 +158,37 @@ private async Task TestSecurityMessageModule(Client.TransportType transport) } } - private async Task SendSingleSecurityMessage(DeviceClient deviceClient, string deviceId, AzureSecurityCenterForIoTLogAnalyticsClient logAnalticsTestClient) + private async Task SendSingleSecurityMessageAsync( + DeviceClient deviceClient, + string deviceId, + AzureSecurityCenterForIoTLogAnalyticsClient logAnalticsTestClient) { await deviceClient.OpenAsync().ConfigureAwait(false); Client.Message testMessage = ComposeD2CSecurityTestMessage(out string eventId, out string payload, out string p1Value); await deviceClient.SendEventAsync(testMessage).ConfigureAwait(false); - await ValidateEvent(deviceId, eventId, payload, p1Value, logAnalticsTestClient).ConfigureAwait(false); + await ValidateEventAsync(deviceId, eventId, payload, p1Value, logAnalticsTestClient).ConfigureAwait(false); } - private Task SendSingleSecurityMessageModule(ModuleClient moduleClient, string deviceId, AzureSecurityCenterForIoTLogAnalyticsClient logAnalticsTestClient) + private async Task SendSingleSecurityMessageModuleAsync( + ModuleClient moduleClient, + string deviceId, + AzureSecurityCenterForIoTLogAnalyticsClient logAnalticsTestClient) { - moduleClient.OpenAsync(); + await moduleClient.OpenAsync().ConfigureAwait(false); Client.Message testMessage = ComposeD2CSecurityTestMessage(out string eventId, out string payload, out string p1Value); - moduleClient.SendEventAsync(testMessage); + await moduleClient.SendEventAsync(testMessage).ConfigureAwait(false); - return ValidateEvent(deviceId, eventId, payload, p1Value, logAnalticsTestClient); + await ValidateEventAsync(deviceId, eventId, payload, p1Value, logAnalticsTestClient).ConfigureAwait(false); } - private async Task ValidateEvent(string deviceId, string eventId, string payload, string p1Value, + private async Task ValidateEventAsync( + string deviceId, + string eventId, + string payload, + string p1Value, AzureSecurityCenterForIoTLogAnalyticsClient logAnalticsTestClient) { bool isReceivedEventHub = EventHubTestListener.VerifyIfMessageIsReceived(deviceId, payload, p1Value, TimeSpan.FromSeconds(10)); @@ -197,7 +207,17 @@ protected virtual void Dispose(bool disposing) { if (disposing) { - _logAnalyticsClient.Dispose(); + if (_logAnalyticsClient != null) + { + _logAnalyticsClient.Dispose(); + _logAnalyticsClient = null; + } + + if (_listener != null) + { + _listener.Dispose(); + _listener = null; + } } } } diff --git a/e2e/test/FileUploadFaultInjectionTests.cs b/e2e/test/FileUploadFaultInjectionTests.cs index d01a8b6e3c..7f144c8ccb 100644 --- a/e2e/test/FileUploadFaultInjectionTests.cs +++ b/e2e/test/FileUploadFaultInjectionTests.cs @@ -1,16 +1,12 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System; -using System.Diagnostics; -using System.IO; -using System.Security.Cryptography.X509Certificates; -using System.Threading; -using System.Threading.Tasks; using Microsoft.Azure.Devices.Client; using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Diagnostics.CodeAnalysis; +using System; using System.Diagnostics.Tracing; +using System.IO; +using System.Threading.Tasks; namespace Microsoft.Azure.Devices.E2ETests { @@ -24,7 +20,7 @@ public class FileUploadFaultInjectionTests : IDisposable private const int FileSizeSmall = 10 * 1024; private const int FileSizeBig = 5120 * 1024; - private readonly ConsoleEventListener _listener; + private ConsoleEventListener _listener; public FileUploadFaultInjectionTests() { @@ -36,12 +32,13 @@ public async Task FileUploadSuccess_TcpLoss_Amqp() { string bigFile = await GetTestFileNameAsync(FileSizeBig).ConfigureAwait(false); - await UploadFileDisconnectTransport(Client.TransportType.Amqp_Tcp_Only, - bigFile, - FaultInjection.FaultType_Tcp, - FaultInjection.FaultCloseReason_Boom, - FaultInjection.DefaultDelayInSec - ).ConfigureAwait(false); + await UploadFileDisconnectTransport( + Client.TransportType.Amqp_Tcp_Only, + bigFile, + FaultInjection.FaultType_Tcp, + FaultInjection.FaultCloseReason_Boom, + FaultInjection.DefaultDelayInSec) + .ConfigureAwait(false); } [TestMethod] @@ -49,13 +46,14 @@ public async Task FileUploadSuccess_Throttled_Amqp() { string smallFile = await GetTestFileNameAsync(FileSizeSmall).ConfigureAwait(false); - await UploadFileDisconnectTransport(Client.TransportType.Amqp_Tcp_Only, - smallFile, - FaultInjection.FaultType_Throttle, - FaultInjection.FaultCloseReason_Boom, - FaultInjection.DefaultDelayInSec, - FaultInjection.DefaultDurationInSec - ).ConfigureAwait(false); + await UploadFileDisconnectTransport( + Client.TransportType.Amqp_Tcp_Only, + smallFile, + FaultInjection.FaultType_Throttle, + FaultInjection.FaultCloseReason_Boom, + FaultInjection.DefaultDelayInSec, + FaultInjection.DefaultDurationInSec) + .ConfigureAwait(false); } [TestMethod] @@ -64,13 +62,14 @@ public async Task FileUploadSuccess_QuotaExceed_Amqp() { string smallFile = await GetTestFileNameAsync(FileSizeSmall).ConfigureAwait(false); - await UploadFileDisconnectTransport(Client.TransportType.Amqp_Tcp_Only, - smallFile, - FaultInjection.FaultType_QuotaExceeded, - FaultInjection.FaultCloseReason_Boom, - FaultInjection.DefaultDelayInSec, - FaultInjection.DefaultDurationInSec - ).ConfigureAwait(false); + await UploadFileDisconnectTransport( + Client.TransportType.Amqp_Tcp_Only, + smallFile, + FaultInjection.FaultType_QuotaExceeded, + FaultInjection.FaultCloseReason_Boom, + FaultInjection.DefaultDelayInSec, + FaultInjection.DefaultDurationInSec) + .ConfigureAwait(false); } private async Task UploadFileDisconnectTransport( @@ -84,17 +83,17 @@ private async Task UploadFileDisconnectTransport( { TestDevice testDevice = await TestDevice.GetTestDeviceAsync(DevicePrefix).ConfigureAwait(false); - using (DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(testDevice.ConnectionString, transport)) + using (var deviceClient = DeviceClient.CreateFromConnectionString(testDevice.ConnectionString, transport)) { deviceClient.OperationTimeoutInMilliseconds = (uint)retryDurationInMilliSec; await FileNotificationTestListener.InitAsync().ConfigureAwait(false); - using (FileStream fileStreamSource = new FileStream(filename, FileMode.Open, FileAccess.Read)) + using (var fileStreamSource = new FileStream(filename, FileMode.Open, FileAccess.Read)) { - Task fileuploadTask = deviceClient.UploadToBlobAsync(filename, fileStreamSource); + Task fileUploadTask = deviceClient.UploadToBlobAsync(filename, fileStreamSource); Task errorInjectionTask = SendErrorInjectionMessageAsync(deviceClient, faultType, reason, delayInSec, durationInSec); - await Task.WhenAll(fileuploadTask, errorInjectionTask).ConfigureAwait(false); + await Task.WhenAll(fileUploadTask, errorInjectionTask).ConfigureAwait(false); await FileNotificationTestListener.VerifyFileNotification(filename, testDevice.Id).ConfigureAwait(false); } @@ -103,7 +102,7 @@ private async Task UploadFileDisconnectTransport( { await deviceClient.CloseAsync().ConfigureAwait(false); } - catch (Exception) + catch { // catch and ignore exceptions resulted incase device client close failed while offline } @@ -121,13 +120,13 @@ private static async Task SendErrorInjectionMessageAsync( { await deviceClient.SendEventAsync(FaultInjection.ComposeErrorInjectionProperties(faultType, reason, delayInSec, durationInSec)).ConfigureAwait(false); } - catch (Exception) + catch { // catch and ignore exceptions resulted from error injection and continue to check result of the file upload status } } - private async Task GetTestFileNameAsync(int fileSize) + private static async Task GetTestFileNameAsync(int fileSize) { var rnd = new Random(); byte[] buffer = new byte[fileSize]; @@ -153,6 +152,11 @@ public void Dispose() protected virtual void Dispose(bool disposing) { + if (_listener != null) + { + _listener.Dispose(); + _listener = null; + } } } }