I have the following method which has a retriable network call. The retry policy is specified for a specific exception.
public async Task<MyResponse> GetRecords(MyRequest request)
{
try
{
RetryPolicy retryPolicy = Policy.Handle<MyException>(ex => ex.ErrorCode == ErrorCode.MySpecificErrorCode)
.Retry(MAX_RETRY_COUNT, onRetry: (exception, retryCount) =>
{
log($"Retrying for {retryCount} times due to my error that I want to retry");
});
return await retryPolicy.Execute(async () =>
await OtherService.NetworkCall(request).ConfigureAwait(false))
.ConfigureAwait(false);
}
catch (Exception ex)
{
log(ex);
throw;
}
}
This is the unit test.
[TestMethod()]
public async Task TestRetry()
{
OtherServiceMock.SetupSequence(x => x.NetworkCall(It.IsAny<MyRequest>()))
.ThrowsAsync(new MyException(ErrorCode.MySpecificErrorCode, ExceptionMessage.MySpecificErrorMessage)) //this is getting thrown
.ThrowsAsync(new Exception());
MyRequest request = new MyRequest();
try
{
var response = await new MyClassMock.GetRecords(request).ConfigureAwait(false);
}
catch(Exception ex)
{
log("Event","Event");
}
}
The first exception is getting thrown instead of the second one. I have tweaked the max retry number, it didn't help. What am I doing wrong here?