-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from Jericho/release/v0.14.0
Release/v0.14.0
- Loading branch information
Showing
9 changed files
with
356 additions
and
231 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
89 changes: 0 additions & 89 deletions
89
Source/StrongGrid.UnitTests/Utilities/AsyncDelayerTests.cs
This file was deleted.
Oops, something went wrong.
177 changes: 177 additions & 0 deletions
177
Source/StrongGrid.UnitTests/Utilities/SendGridRetryStrategyTests.cs
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,177 @@ | ||
using Moq; | ||
using Shouldly; | ||
using StrongGrid.Utilities; | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using Xunit; | ||
|
||
namespace StrongGrid.UnitTests | ||
{ | ||
public class SendGridRetryStrategyTests | ||
{ | ||
[Fact] | ||
public void ShouldRetry_returns_false_when_attempts_equal_max() | ||
{ | ||
// Arrange | ||
var maxAttempts = 5; | ||
var mockSystemClock = new MockSystemClock(2016, 11, 11, 13, 14, 0, 0); | ||
var sendGridRetryStrategy = new SendGridRetryStrategy(maxAttempts, mockSystemClock.Object); | ||
var response = (HttpResponseMessage)null; | ||
|
||
// Act | ||
var result = sendGridRetryStrategy.ShouldRetry(maxAttempts, response); | ||
|
||
// Assert | ||
result.ShouldBeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldRetry_returns_false_when_attempts_exceed_max() | ||
{ | ||
// Arrange | ||
var maxAttempts = 5; | ||
var mockSystemClock = new MockSystemClock(2016, 11, 11, 13, 14, 0, 0); | ||
var sendGridRetryStrategy = new SendGridRetryStrategy(maxAttempts, mockSystemClock.Object); | ||
var response = (HttpResponseMessage)null; | ||
|
||
// Act | ||
var result = sendGridRetryStrategy.ShouldRetry(maxAttempts + 1, response); | ||
|
||
// Assert | ||
result.ShouldBeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldRetry_returns_false_when_previous_response_is_null() | ||
{ | ||
// Arrange | ||
var maxAttempts = 5; | ||
var mockSystemClock = new MockSystemClock(2016, 11, 11, 13, 14, 0, 0); | ||
var sendGridRetryStrategy = new SendGridRetryStrategy(maxAttempts, mockSystemClock.Object); | ||
var response = (HttpResponseMessage)null; | ||
|
||
// Act | ||
var result = sendGridRetryStrategy.ShouldRetry(1, response); | ||
|
||
// Assert | ||
result.ShouldBeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldRetry_returns_true_when_statuscode_429() | ||
{ | ||
// Arrange | ||
var maxAttempts = 5; | ||
var mockSystemClock = new MockSystemClock(2016, 11, 11, 13, 14, 0, 0); | ||
var sendGridRetryStrategy = new SendGridRetryStrategy(maxAttempts, mockSystemClock.Object); | ||
var response = new HttpResponseMessage((HttpStatusCode)429); | ||
|
||
// Act | ||
var result = sendGridRetryStrategy.ShouldRetry(1, response); | ||
|
||
// Assert | ||
result.ShouldBeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldRetry_returns_false_when_statuscode_not_429() | ||
{ | ||
// Arrange | ||
var maxAttempts = 5; | ||
var mockSystemClock = new MockSystemClock(2016, 11, 11, 13, 14, 0, 0); | ||
var sendGridRetryStrategy = new SendGridRetryStrategy(maxAttempts, mockSystemClock.Object); | ||
var response = new HttpResponseMessage(HttpStatusCode.BadGateway); | ||
|
||
// Act | ||
var result = sendGridRetryStrategy.ShouldRetry(1, response); | ||
|
||
// Assert | ||
result.ShouldBeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void GetNextDelay_with_null_HttpHeaders() | ||
{ | ||
// Arrange | ||
var maxAttempts = 5; | ||
var mockSystemClock = new MockSystemClock(2016, 11, 11, 13, 14, 0, 0); | ||
var sendGridRetryStrategy = new SendGridRetryStrategy(maxAttempts, mockSystemClock.Object); | ||
var response = (HttpResponseMessage)null; | ||
|
||
// Act | ||
var result = sendGridRetryStrategy.GetNextDelay(1, response); | ||
|
||
// Assert | ||
result.ShouldBe(TimeSpan.FromSeconds(1)); | ||
} | ||
|
||
[Fact] | ||
public void CalculateDelay_without_XRateLimitReset() | ||
{ | ||
// Arrange | ||
var maxAttempts = 5; | ||
var mockSystemClock = new MockSystemClock(2016, 11, 11, 13, 14, 0, 0); | ||
var sendGridRetryStrategy = new SendGridRetryStrategy(maxAttempts, mockSystemClock.Object); | ||
var response = new HttpResponseMessage((HttpStatusCode)428); | ||
|
||
// Act | ||
var result = sendGridRetryStrategy.GetNextDelay(1, response); | ||
|
||
// Assert | ||
result.ShouldBe(TimeSpan.FromSeconds(1)); | ||
} | ||
|
||
[Fact] | ||
public void CalculateDelay_with_too_small_XRateLimitReset() | ||
{ | ||
// Arrange | ||
var maxAttempts = 5; | ||
var mockSystemClock = new MockSystemClock(2016, 11, 11, 13, 14, 0, 0); | ||
var sendGridRetryStrategy = new SendGridRetryStrategy(maxAttempts, mockSystemClock.Object); | ||
var response = new HttpResponseMessage((HttpStatusCode)428); | ||
response.Headers.Add("X-RateLimit-Reset", "-1"); | ||
|
||
// Act | ||
var result = sendGridRetryStrategy.GetNextDelay(1, response); | ||
|
||
// Assert | ||
result.ShouldBe(TimeSpan.FromSeconds(1)); | ||
} | ||
|
||
[Fact] | ||
public void CalculateDelay_with_reasonable_XRateLimitReset() | ||
{ | ||
// Arrange | ||
var maxAttempts = 5; | ||
var mockSystemClock = new MockSystemClock(2016, 11, 11, 13, 14, 0, 0); | ||
var sendGridRetryStrategy = new SendGridRetryStrategy(maxAttempts, mockSystemClock.Object); | ||
var response = new HttpResponseMessage((HttpStatusCode)428); | ||
response.Headers.Add("X-RateLimit-Reset", mockSystemClock.Object.UtcNow.AddSeconds(3).ToUnixTime().ToString()); | ||
|
||
// Act | ||
var result = sendGridRetryStrategy.GetNextDelay(1, response); | ||
|
||
// Assert | ||
result.ShouldBe(TimeSpan.FromSeconds(3)); | ||
} | ||
|
||
[Fact] | ||
public void CalculateDelay_with_too_large_XRateLimitReset() | ||
{ | ||
// Arrange | ||
var maxAttempts = 5; | ||
var mockSystemClock = new MockSystemClock(2016, 11, 11, 13, 14, 0, 0); | ||
var sendGridRetryStrategy = new SendGridRetryStrategy(maxAttempts, mockSystemClock.Object); | ||
var response = new HttpResponseMessage((HttpStatusCode)428); | ||
response.Headers.Add("X-RateLimit-Reset", mockSystemClock.Object.UtcNow.AddHours(1).ToUnixTime().ToString()); | ||
|
||
// Act | ||
var result = sendGridRetryStrategy.GetNextDelay(1, response); | ||
|
||
// Assert | ||
result.ShouldBe(TimeSpan.FromSeconds(5)); | ||
} | ||
} | ||
} |
Oops, something went wrong.