Skip to content

Commit

Permalink
Test code cleanup (#1219)
Browse files Browse the repository at this point in the history
Co-authored-by: Abhipsa Misra
  • Loading branch information
David R. Williamson authored Feb 18, 2020
1 parent b5c05c3 commit c90cd00
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 63 deletions.
3 changes: 3 additions & 0 deletions CodeMaid.config
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<setting name="Reorganizing_PrimaryOrderByAccessLevel" serializeAs="String">
<value>True</value>
</setting>
<setting name="Cleaning_PerformPartialCleanupOnExternal" serializeAs="String">
<value>1</value>
</setting>
</SteveCadwallader.CodeMaid.Properties.Settings>
</userSettings>
</configuration>
2 changes: 1 addition & 1 deletion common/test/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
74 changes: 47 additions & 27 deletions e2e/test/AzureSecurityCenterForIoTSecurityMessageE2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -33,63 +32,63 @@ 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)
{
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 }
};
Expand All @@ -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" },
Expand All @@ -124,15 +124,15 @@ 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);

using (DeviceClient deviceClient = testDevice.CreateDeviceClient(transport))
{
try
{
await SendSingleSecurityMessage(deviceClient, testDevice.Id, _logAnalyticsClient).ConfigureAwait(false);
await SendSingleSecurityMessageAsync(deviceClient, testDevice.Id, _logAnalyticsClient).ConfigureAwait(false);
}
finally
{
Expand All @@ -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
{
Expand All @@ -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));
Expand All @@ -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;
}
}
}
}
Expand Down
74 changes: 39 additions & 35 deletions e2e/test/FileUploadFaultInjectionTests.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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()
{
Expand All @@ -36,26 +32,28 @@ 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]
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]
Expand All @@ -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(
Expand All @@ -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);
}
Expand All @@ -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
}
Expand All @@ -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<string> GetTestFileNameAsync(int fileSize)
private static async Task<string> GetTestFileNameAsync(int fileSize)
{
var rnd = new Random();
byte[] buffer = new byte[fileSize];
Expand All @@ -153,6 +152,11 @@ public void Dispose()

protected virtual void Dispose(bool disposing)
{
if (_listener != null)
{
_listener.Dispose();
_listener = null;
}
}
}
}

0 comments on commit c90cd00

Please sign in to comment.