Skip to content

Commit

Permalink
Update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
schoims committed Jan 19, 2023
1 parent 3c9bad7 commit 6cd36f1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 47 deletions.
4 changes: 2 additions & 2 deletions iothub/device/src/Pipeline/DefaultDelegatingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ public virtual Task<long> UpdateReportedPropertiesAsync(ReportedProperties repor
public virtual Task<DateTime> RefreshSasTokenAsync(CancellationToken cancellationToken)
{
ThrowIfDisposed();
return NextHandler?.RefreshSasTokenAsync(cancellationToken) ?? Task.FromResult(DateTime.Now);
return NextHandler?.RefreshSasTokenAsync(cancellationToken) ?? Task.FromResult(DateTime.UtcNow);
}

public virtual DateTime GetSasTokenRefreshesOn()
{
ThrowIfDisposed();
return NextHandler?.GetSasTokenRefreshesOn() ?? DateTime.MaxValue;
return NextHandler?.GetSasTokenRefreshesOn() ?? DateTime.UtcNow;
}

public virtual void SetSasTokenRefreshesOn()
Expand Down
4 changes: 1 addition & 3 deletions iothub/device/src/Pipeline/RetryDelegatingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@ public override async Task<DateTime> RefreshSasTokenAsync(CancellationToken canc
await VerifyIsOpenAsync(cancellationToken).ConfigureAwait(false);
return await base.RefreshSasTokenAsync(cancellationToken).ConfigureAwait(false);
},
(Exception ex) => ex is IotHubClientException iex && iex.ErrorCode == IotHubClientErrorCode.NetworkErrors,
cancellationToken)
.ConfigureAwait(false);
}
Expand Down Expand Up @@ -602,14 +601,13 @@ private async Task RefreshSasTokenLoopAsync(DateTime refreshesOn, CancellationTo

refreshesOn = await RefreshSasTokenAsync(cancellationToken).ConfigureAwait(false);

// what happens if refreshesOn is DateTime.MaxValue ?

if (Logging.IsEnabled)
Logging.Info(this, refreshesOn, "Token has been refreshed.");

waitTime = refreshesOn - DateTime.UtcNow;
}
}
// OperationCanceledException can be thrown when the connection is closing or the cancellationToken is signaled
catch (OperationCanceledException) { return; }
finally
{
Expand Down
30 changes: 0 additions & 30 deletions iothub/device/src/Transport/Amqp/AmqpAuthenticationRefresher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Diagnostics;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -87,34 +86,5 @@ private static string CreateAmqpCbsAudience(IConnectionCredentials connectionCre
// SAS tokens granted to a group SAS authenticated client will scoped to the IoT hub-level; for example, myHub.azure-devices.net
return connectionCredentials.HostName;
}

//public void Dispose()
//{
// Dispose(true);
// GC.SuppressFinalize(this);
//}

//private void Dispose(bool disposing)
//{
// try
// {
// if (Logging.IsEnabled)
// Logging.Enter(this, $"Disposed={_disposed}; disposing={disposing}", $"{nameof(AmqpAuthenticationRefresher)}.{nameof(Dispose)}");

// if (!_disposed)
// {
// if (disposing)
// {
// }

// _disposed = true;
// }
// }
// finally
// {
// if (Logging.IsEnabled)
// Logging.Exit(this, $"Disposed={_disposed}; disposing={disposing}", $"{nameof(AmqpAuthenticationRefresher)}.{nameof(Dispose)}");
// }
//}
}
}
37 changes: 25 additions & 12 deletions iothub/device/tests/Pipeline/RetryDelegatingHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,13 @@ public async Task RetrySetRetryPolicyVerifyInternalsSuccess()
}

[TestMethod]
public async Task RetryDelegatingHandler_RefreshTokenAsync_DoesNotRetryOnNonNetworkErrorCode()
[DataRow(IotHubClientErrorCode.NetworkErrors)]
[DataRow(IotHubClientErrorCode.Throttled)]
[DataRow(IotHubClientErrorCode.QuotaExceeded)]
[DataRow(IotHubClientErrorCode.ServerError)]
[DataRow(IotHubClientErrorCode.ServerBusy)]
[DataRow(IotHubClientErrorCode.Timeout)]
public async Task RetryDelegatingHandler_RefreshTokenAsync_RetriesOnTransientErrors(IotHubClientErrorCode errorCode)
{
// arrange
int callCounter = 0;
Expand All @@ -478,7 +484,7 @@ public async Task RetryDelegatingHandler_RefreshTokenAsync_DoesNotRetryOnNonNetw
{
if (++callCounter == 1)
{
throw new IotHubClientException("Device not found", IotHubClientErrorCode.DeviceNotFound);
throw new IotHubClientException("Transient error occurred", errorCode);
}
return Task.FromResult(DateTime.UtcNow);
});
Expand All @@ -487,17 +493,22 @@ public async Task RetryDelegatingHandler_RefreshTokenAsync_DoesNotRetryOnNonNetw

// act
await retryDelegatingHandler.OpenAsync(CancellationToken.None).ConfigureAwait(false);
Func<Task> refreshToken = () => retryDelegatingHandler.RefreshSasTokenAsync(CancellationToken.None);
await retryDelegatingHandler.RefreshSasTokenAsync(CancellationToken.None).ConfigureAwait(false);

// assert
await refreshToken.Should()
.ThrowAsync<IotHubClientException>()
.ConfigureAwait(false);
callCounter.Should().Be(1);
callCounter.Should().Be(2);
}

[TestMethod]
public async Task RetryDelegatingHandler_RefreshTokenAsync_Retries()
[DataRow(IotHubClientErrorCode.Unknown)]
[DataRow(IotHubClientErrorCode.DeviceMessageLockLost)]
[DataRow(IotHubClientErrorCode.DeviceNotFound)]
[DataRow(IotHubClientErrorCode.Suspended)]
[DataRow(IotHubClientErrorCode.PreconditionFailed)]
[DataRow(IotHubClientErrorCode.Unauthorized)]
[DataRow(IotHubClientErrorCode.TlsAuthenticationError)]
[DataRow(IotHubClientErrorCode.ArgumentInvalid)]
public async Task RetryDelegatingHandler_RefreshTokenAsync_NoRetriesOnNonTransientErrors(IotHubClientErrorCode errorCode)
{
// arrange
int callCounter = 0;
Expand All @@ -520,7 +531,7 @@ public async Task RetryDelegatingHandler_RefreshTokenAsync_Retries()
{
if (++callCounter == 1)
{
throw new IotHubClientException("network error", IotHubClientErrorCode.NetworkErrors);
throw new IotHubClientException("Non-transient error occurred", errorCode);
}
return Task.FromResult(DateTime.UtcNow);
});
Expand All @@ -529,12 +540,14 @@ public async Task RetryDelegatingHandler_RefreshTokenAsync_Retries()

// act
await retryDelegatingHandler.OpenAsync(CancellationToken.None).ConfigureAwait(false);
await retryDelegatingHandler.RefreshSasTokenAsync(CancellationToken.None).ConfigureAwait(false);
Func<Task> refreshToken = () => retryDelegatingHandler.RefreshSasTokenAsync(CancellationToken.None);

// assert
callCounter.Should().Be(2);
await refreshToken.Should()
.ThrowAsync<IotHubClientException>()
.ConfigureAwait(false);
callCounter.Should().Be(1);
}

private class TestRetryPolicy : IIotHubClientRetryPolicy
{
public uint Counter { get; private set; }
Expand Down

0 comments on commit 6cd36f1

Please sign in to comment.