Skip to content

Commit 58e95c7

Browse files
committed
Renamed for clarity (+1 squashed commits)
Squashed commits: [e86f374] Made access private to GetNextWaitInterval (+1 squashed commits) Squashed commits: [462983b] Renamed some unit tests
1 parent 2c02990 commit 58e95c7

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

src/SendGrid/Reliability/RetryDelegatingHandler.cs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class RetryDelegatingHandler : DelegatingHandler
1515

1616
private readonly ReliabilitySettings settings;
1717

18+
private readonly Random random = new Random();
19+
1820
/// <summary>
1921
/// Initializes a new instance of the <see cref="RetryDelegatingHandler"/> class.
2022
/// </summary>
@@ -38,7 +40,6 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
3840
}
3941

4042
HttpResponseMessage responseMessage = null;
41-
var backOffCalculator = new ExponentialBackOffCalculator(settings.RetryInterval);
4243

4344
var waitFor = settings.RetryInterval;
4445
var numberOfAttempts = 0;
@@ -50,7 +51,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
5051
{
5152
responseMessage = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
5253

53-
ThrowHttpRequestExceptionIfResponseCodeIsRetriable(responseMessage);
54+
ThrowHttpRequestExceptionIfResponseCodeCanBeRetried(responseMessage);
5455

5556
sent = true;
5657
}
@@ -78,13 +79,13 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
7879
await Task.Delay(waitFor).ConfigureAwait(false);
7980
}
8081

81-
waitFor = backOffCalculator.GetNextWaitInterval(numberOfAttempts);
82+
waitFor = GetNextWaitInterval(numberOfAttempts);
8283
}
8384

8485
return responseMessage;
8586
}
8687

87-
private static void ThrowHttpRequestExceptionIfResponseCodeIsRetriable(HttpResponseMessage responseMessage)
88+
private static void ThrowHttpRequestExceptionIfResponseCodeCanBeRetried(HttpResponseMessage responseMessage)
8889
{
8990
int statusCode = (int)responseMessage.StatusCode;
9091

@@ -94,25 +95,13 @@ private static void ThrowHttpRequestExceptionIfResponseCodeIsRetriable(HttpRespo
9495
}
9596
}
9697

97-
private class ExponentialBackOffCalculator
98+
private TimeSpan GetNextWaitInterval(int numberOfAttempts)
9899
{
99-
private TimeSpan baseInterval;
100-
101-
private readonly Random random = new Random();
102-
103-
public ExponentialBackOffCalculator(TimeSpan baseInterval)
104-
{
105-
this.baseInterval = baseInterval;
106-
}
107-
108-
public TimeSpan GetNextWaitInterval(int numberOfAttempts)
109-
{
110-
var interval = this.baseInterval.TotalMilliseconds + (Math.Pow(2, numberOfAttempts) * 1000);
100+
var interval = this.settings.RetryInterval.TotalMilliseconds + (Math.Pow(2, numberOfAttempts) * 1000);
111101

112-
var randomDelay = this.random.Next(0, 1000);
102+
var randomDelay = this.random.Next(0, 1000);
113103

114-
return TimeSpan.FromMilliseconds(interval + randomDelay);
115-
}
104+
return TimeSpan.FromMilliseconds(interval + randomDelay);
116105
}
117106
}
118107
}

tests/SendGrid.Tests/Reliability/RetryDelegatingHandlerTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public RetryDelegatingHandlerTests()
2727
}
2828

2929
[Fact]
30-
public async Task Invoke_ShouldReturnHttpResponseAndNotRetryWhenSuccessful()
30+
public async Task ShouldReturnHttpResponseAndNotRetryWhenSuccessful()
3131
{
3232
innerHandler.AddBehaviour(innerHandler.OK);
3333

@@ -38,7 +38,7 @@ public async Task Invoke_ShouldReturnHttpResponseAndNotRetryWhenSuccessful()
3838
}
3939

4040
[Fact]
41-
public async Task Invoke_ShouldReturnHttpResponseAndNotRetryWhenUnauthorised()
41+
public async Task ShouldReturnHttpResponseAndNotRetryWhenUnauthorised()
4242
{
4343
innerHandler.AddBehaviour(innerHandler.AuthenticationError);
4444

@@ -49,7 +49,7 @@ public async Task Invoke_ShouldReturnHttpResponseAndNotRetryWhenUnauthorised()
4949
}
5050

5151
[Fact]
52-
public async Task Invoke_ShouldReturnErrorWithoutRetryWhenErrorIsNotTransient()
52+
public async Task ShouldReturnErrorWithoutRetryWhenErrorIsNotTransient()
5353
{
5454
innerHandler.AddBehaviour(innerHandler.NonTransientException);
5555

@@ -59,7 +59,7 @@ public async Task Invoke_ShouldReturnErrorWithoutRetryWhenErrorIsNotTransient()
5959
}
6060

6161
[Fact]
62-
public async Task Invoke_ShouldReturnErrorWithoutRetryWhen500ErrorStatusIsNotTransient()
62+
public async Task ShouldReturnErrorWithoutRetryWhen500ErrorStatusIsNotTransient()
6363
{
6464
innerHandler.AddBehaviour(innerHandler.HttpVersionNotSupported);
6565

@@ -70,7 +70,7 @@ public async Task Invoke_ShouldReturnErrorWithoutRetryWhen500ErrorStatusIsNotTra
7070
}
7171

7272
[Fact]
73-
public async Task Invoke_ShouldReturnErrorWithoutRetryWhen501ErrorStatus()
73+
public async Task ShouldReturnErrorWithoutRetryWhen501ErrorStatus()
7474
{
7575
innerHandler.AddBehaviour(innerHandler.NotImplemented);
7676

@@ -81,7 +81,7 @@ public async Task Invoke_ShouldReturnErrorWithoutRetryWhen501ErrorStatus()
8181
}
8282

8383
[Fact]
84-
public async Task Invoke_ShouldRetryOnceWhenFailedOnFirstAttemptThenSuccessful()
84+
public async Task ShouldRetryOnceWhenFailedOnFirstAttemptThenSuccessful()
8585
{
8686
innerHandler.AddBehaviour(innerHandler.TaskCancelled);
8787
innerHandler.AddBehaviour(innerHandler.OK);
@@ -93,7 +93,7 @@ public async Task Invoke_ShouldRetryOnceWhenFailedOnFirstAttemptThenSuccessful()
9393
}
9494

9595
[Fact]
96-
public async Task Invoke_ShouldRetryTheExpectedAmountOfTimesAndReturnTimeoutExceptionWhenTasksCancelled()
96+
public async Task ShouldRetryTheExpectedAmountOfTimesAndReturnTimeoutExceptionWhenTasksCancelled()
9797
{
9898
innerHandler.AddBehaviour(innerHandler.TaskCancelled);
9999
innerHandler.AddBehaviour(innerHandler.TaskCancelled);
@@ -104,7 +104,7 @@ public async Task Invoke_ShouldRetryTheExpectedAmountOfTimesAndReturnTimeoutExce
104104
}
105105

106106
[Fact]
107-
public async Task Invoke_ShouldRetryTheExpectedAmountOfTimesAndReturnExceptionWhenInternalServerErrorsEncountered()
107+
public async Task ShouldRetryTheExpectedAmountOfTimesAndReturnExceptionWhenInternalServerErrorsEncountered()
108108
{
109109
innerHandler.AddBehaviour(innerHandler.InternalServerError);
110110
innerHandler.AddBehaviour(innerHandler.ServiceUnavailable);

0 commit comments

Comments
 (0)