Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flaky test fix and wait on some fault injection durations #2798

Merged
merged 6 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions e2e/test/helpers/templates/FaultInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ public static bool FaultShouldDisconnect(string faultType)

deviceClient.Dispose();
await testDevice.RemoveDeviceAsync().ConfigureAwait(false);

if (!FaultShouldDisconnect(faultType))
{
faultInjectionDuration.Stop();

TimeSpan timeToFinishFaultInjection = faultDuration.Subtract(faultInjectionDuration.Elapsed);
if (timeToFinishFaultInjection > TimeSpan.Zero)
{
logger.Trace($"{nameof(FaultInjection)}: Waiting {timeToFinishFaultInjection} to ensure that FaultInjection duration passed.");
await Task.Delay(timeToFinishFaultInjection).ConfigureAwait(false);
}
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions e2e/test/helpers/templates/FaultInjectionPoolingOverAmqp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ internal static class FaultInjectionPoolingOverAmqp
testDeviceCallbackHandlers.ForEach(x => x.Dispose());
deviceClients.ForEach(x => x.Dispose());
await Task.WhenAll(testDevices.Select(x => x.RemoveDeviceAsync())).ConfigureAwait(false);

if (!FaultInjection.FaultShouldDisconnect(faultType))
{
faultInjectionDuration.Stop();

TimeSpan timeToFinishFaultInjection = faultDuration.Subtract(faultInjectionDuration.Elapsed);
if (timeToFinishFaultInjection > TimeSpan.Zero)
{
logger.Trace($"{nameof(FaultInjection)}: Waiting {timeToFinishFaultInjection} to ensure that FaultInjection duration passed.");
await Task.Delay(timeToFinishFaultInjection).ConfigureAwait(false);
}
}
}
}
}
Expand Down
71 changes: 38 additions & 33 deletions e2e/test/iothub/service/IoTHubServiceProxyE2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,65 +49,70 @@ public async Task RegistryManager_AddAndRemoveDevice_WithProxy()
}

[LoggedTestMethod]
[Timeout(TestTimeoutMilliseconds)]
[TestCategory("LongRunning")]
[Timeout(LongRunningTestTimeoutMilliseconds)]
public async Task JobClient_ScheduleAndRunTwinJob_WithProxy()
{
var httpTransportSettings = new HttpTransportSettings();
httpTransportSettings.Proxy = new WebProxy(s_proxyServerAddress);
var httpTransportSettings = new HttpTransportSettings
{
Proxy = new WebProxy(s_proxyServerAddress)
};

await JobClient_ScheduleAndRunTwinJob(httpTransportSettings).ConfigureAwait(false);
}

private async Task SendSingleMessageService(ServiceClientTransportSettings transportSettings)
{
using TestDevice testDevice = await TestDevice.GetTestDeviceAsync(Logger, DevicePrefix).ConfigureAwait(false);
using (var deviceClient = DeviceClient.CreateFromConnectionString(testDevice.ConnectionString))
using (var serviceClient = ServiceClient.CreateFromConnectionString(s_connectionString, TransportType.Amqp, transportSettings))
using var deviceClient = DeviceClient.CreateFromConnectionString(testDevice.ConnectionString);
using var serviceClient = ServiceClient.CreateFromConnectionString(s_connectionString, TransportType.Amqp, transportSettings);
(Message testMessage, string messageId, string payload, string p1Value) = ComposeD2CTestMessage();

using (testMessage)
{
(Message testMessage, string messageId, string payload, string p1Value) = ComposeD2CTestMessage();
await serviceClient.SendAsync(testDevice.Id, testMessage).ConfigureAwait(false);

await deviceClient.CloseAsync().ConfigureAwait(false);
await serviceClient.CloseAsync().ConfigureAwait(false);
}

await deviceClient.CloseAsync().ConfigureAwait(false);
await serviceClient.CloseAsync().ConfigureAwait(false);
}

private async Task RegistryManager_AddDevice(HttpTransportSettings httpTransportSettings)
{
string deviceName = DevicePrefix + Guid.NewGuid();

using (var registryManager = RegistryManager.CreateFromConnectionString(s_connectionString, httpTransportSettings))
{
await registryManager.AddDeviceAsync(new Device(deviceName)).ConfigureAwait(false);
await registryManager.RemoveDeviceAsync(deviceName).ConfigureAwait(false);
}
using var registryManager = RegistryManager.CreateFromConnectionString(s_connectionString, httpTransportSettings);
await registryManager.AddDeviceAsync(new Device(deviceName)).ConfigureAwait(false);
await registryManager.RemoveDeviceAsync(deviceName).ConfigureAwait(false);
}

private async Task JobClient_ScheduleAndRunTwinJob(HttpTransportSettings httpTransportSettings)
{
var twin = new Twin(JobDeviceId);
twin.Tags = new TwinCollection();
var twin = new Twin(JobDeviceId)
{
Tags = new TwinCollection()
};
twin.Tags[JobTestTagName] = JobDeviceId;

using (var jobClient = JobClient.CreateFromConnectionString(s_connectionString, httpTransportSettings))
using var jobClient = JobClient.CreateFromConnectionString(s_connectionString, httpTransportSettings);
int tryCount = 0;
while (true)
{
int tryCount = 0;
while (true)
try
{
string jobId = "JOBSAMPLE" + Guid.NewGuid().ToString();
string query = $"DeviceId IN ['{JobDeviceId}']";
JobResponse createJobResponse = await jobClient
.ScheduleTwinUpdateAsync(jobId, query, twin, DateTime.UtcNow, (long)TimeSpan.FromMinutes(2).TotalSeconds)
.ConfigureAwait(false);
break;
}
// Concurrent jobs can be rejected, so implement a retry mechanism to handle conflicts with other tests
catch (ThrottlingException) when (++tryCount < MaxIterationWait)
{
try
{
string jobId = "JOBSAMPLE" + Guid.NewGuid().ToString();
string query = $"DeviceId IN ['{JobDeviceId}']";
JobResponse createJobResponse = await jobClient.ScheduleTwinUpdateAsync(jobId, query, twin, DateTime.UtcNow, (long)TimeSpan.FromMinutes(2).TotalSeconds).ConfigureAwait(false);
break;
}
// Concurrent jobs can be rejected, so implement a retry mechanism to handle conflicts with other tests
catch (ThrottlingException) when (++tryCount < MaxIterationWait)
{
Logger.Trace($"ThrottlingException... waiting.");
await Task.Delay(_waitDuration).ConfigureAwait(false);
continue;
}
Logger.Trace($"ThrottlingException... waiting.");
await Task.Delay(_waitDuration).ConfigureAwait(false);
continue;
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion e2e/test/iothub/service/RegistryManagerE2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,14 @@ public async Task RegistryManager_AddDeviceWithTwinWithDeviceCapabilities()

try
{
Device actual = await registryManager.GetDeviceAsync(deviceId).ConfigureAwait(false);
Device actual = null;
do
{
// allow some time for the device registry to update the cache
await Task.Delay(50).ConfigureAwait(false);
actual = await registryManager.GetDeviceAsync(deviceId).ConfigureAwait(false);
} while (actual == null);

actual.Should().NotBeNull($"Got null in GET on device {deviceId} to check IotEdge property.");
actual.Capabilities.IotEdge.Should().BeTrue();
}
Expand Down
5 changes: 3 additions & 2 deletions iothub/device/tests/Pipeline/RetryDelegatingHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,16 @@ public async Task RetrySetRetryPolicyVerifyInternalsSuccess()
// arrange
var innerHandlerMock = Substitute.For<IDelegatingHandler>();
var contextMock = Substitute.For<PipelineContext>();
contextMock.ConnectionStatusChangesHandler = new ConnectionStatusChangesHandler(delegate (ConnectionStatus status, ConnectionStatusChangeReason reason) { });
contextMock.ConnectionStatusChangesHandler = new ConnectionStatusChangesHandler(
delegate (ConnectionStatus status, ConnectionStatusChangeReason reason) { });
var sut = new RetryDelegatingHandler(contextMock, innerHandlerMock);

var retryPolicy = new TestRetryPolicy();
sut.SetRetryPolicy(retryPolicy);

int innerHandlerCallCounter = 0;

innerHandlerMock.OpenAsync(Arg.Any<CancellationToken>()).Returns(t =>
innerHandlerMock.OpenAsync(CancellationToken.None).Returns(t =>
{
innerHandlerCallCounter++;
throw new IotHubCommunicationException();
Expand Down