-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David R. Williamson
committed
Oct 20, 2022
1 parent
8657ba9
commit cce60e5
Showing
8 changed files
with
159 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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.Collections.Generic; | ||
using Microsoft.Azure.Devices.Client; | ||
|
||
namespace Microsoft.Azure.Devices.E2ETests.Helpers | ||
{ | ||
internal class HubServiceTestRetryPolicy : IRetryPolicy | ||
{ | ||
private readonly HashSet<IotHubServiceErrorCode> _iotHubServiceErrorCodes; | ||
private const int MaxRetries = 20; | ||
private static readonly TimeSpan s_retryDelay = TimeSpan.FromSeconds(1); | ||
private static readonly TimeSpan s_maxDelay = TimeSpan.FromSeconds(3); | ||
|
||
public HubServiceTestRetryPolicy(HashSet<IotHubServiceErrorCode> iotHubServiceErrorCodes = default) | ||
{ | ||
_iotHubServiceErrorCodes = iotHubServiceErrorCodes ?? new HashSet<IotHubServiceErrorCode>(); | ||
} | ||
|
||
public bool ShouldRetry(uint currentRetryCount, Exception lastException, out TimeSpan retryInterval) | ||
{ | ||
retryInterval = TimeSpan.Zero; | ||
|
||
if (currentRetryCount < MaxRetries) | ||
{ | ||
VerboseTestLogger.WriteLine($"{nameof(HubServiceTestRetryPolicy)}: Exhausted {currentRetryCount}/{MaxRetries} retries and failing due to {lastException}"); | ||
return false; | ||
} | ||
|
||
if (lastException is not IotHubServiceException) | ||
{ | ||
VerboseTestLogger.WriteLine($"{nameof(HubServiceTestRetryPolicy)}: Unretriable exception encountered: {lastException}"); | ||
return false; | ||
} | ||
|
||
var hubEx = (IotHubServiceException)lastException; | ||
|
||
if (hubEx.IsTransient | ||
|| _iotHubServiceErrorCodes.Contains(hubEx.ErrorCode)) | ||
{ | ||
VerboseTestLogger.WriteLine($"{nameof(HubServiceTestRetryPolicy)}: retrying due to transient {hubEx.IsTransient} or error code {hubEx.ErrorCode}."); | ||
double waitDurationMs = Math.Min( | ||
currentRetryCount * s_retryDelay.TotalMilliseconds, | ||
s_maxDelay.TotalMilliseconds); | ||
|
||
retryInterval = TimeSpan.FromMilliseconds(waitDurationMs); | ||
|
||
return true; | ||
} | ||
|
||
VerboseTestLogger.WriteLine($"{nameof(HubServiceTestRetryPolicy)}: not retrying due to transient {hubEx.IsTransient} or error code {hubEx.ErrorCode}."); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.