Skip to content

Commit 572ffaa

Browse files
committed
Fixed some naming rules, move retry tests to reliability directory
1 parent 1e97877 commit 572ffaa

File tree

5 files changed

+72
-32
lines changed

5 files changed

+72
-32
lines changed

src/SendGrid/Helpers/Reliability/ReliabilitySettings.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
using System;
2-
31
namespace SendGrid.Helpers.Reliability
42
{
3+
using System;
4+
55
/// <summary>
66
/// Defines the reliability settings to use on HTTP requests
77
/// </summary>
88
public class ReliabilitySettings
99
{
1010
private int retryCount;
11+
1112
private TimeSpan retryInterval;
1213

1314
/// <summary>
1415
/// Initializes a new instance of the <see cref="ReliabilitySettings"/> class.
1516
/// </summary>
1617
public ReliabilitySettings()
1718
{
18-
retryCount = 0;
19-
retryInterval = TimeSpan.FromSeconds(1);
19+
this.retryCount = 0;
20+
this.retryInterval = TimeSpan.FromSeconds(1);
2021
}
2122

2223
/// <summary>
@@ -26,7 +27,7 @@ public int RetryCount
2627
{
2728
get
2829
{
29-
return retryCount;
30+
return this.retryCount;
3031
}
3132

3233
set
@@ -41,7 +42,7 @@ public int RetryCount
4142
throw new ArgumentException("The maximum number of retries is 5");
4243
}
4344

44-
retryCount = value;
45+
this.retryCount = value;
4546
}
4647
}
4748

@@ -52,7 +53,7 @@ public TimeSpan RetryInterval
5253
{
5354
get
5455
{
55-
return retryInterval;
56+
return this.retryInterval;
5657
}
5758

5859
set
@@ -62,7 +63,7 @@ public TimeSpan RetryInterval
6263
throw new ArgumentException("The maximum retry interval is 30 seconds");
6364
}
6465

65-
retryInterval = value;
66+
this.retryInterval = value;
6667
}
6768
}
6869
}

src/SendGrid/Helpers/Reliability/RetryDelegatingHandler.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
using Polly;
88
using Polly.Retry;
99

10+
/// <summary>
11+
/// A delegating handler that provides retry functionality while executing a request
12+
/// </summary>
1013
public class RetryDelegatingHandler : DelegatingHandler
1114
{
1215
private readonly ReliabilitySettings settings;
@@ -26,19 +29,19 @@ public RetryDelegatingHandler(HttpMessageHandler innerHandler, ReliabilitySettin
2629
: base(innerHandler)
2730
{
2831
this.settings = settings;
29-
ConfigurePolicy();
32+
this.ConfigurePolicy();
3033
}
3134

3235
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
3336
{
34-
if (settings.RetryCount == 0)
37+
if (this.settings.RetryCount == 0)
3538
{
3639
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
3740
}
3841

3942
HttpResponseMessage responseMessage;
4043

41-
var result = await retryPolicy.ExecuteAndCaptureAsync(
44+
var result = await this.retryPolicy.ExecuteAndCaptureAsync(
4245
async () =>
4346
{
4447
try
@@ -72,7 +75,7 @@ private static void ThrowHttpRequestExceptionIfResponseIsWithinTheServerErrorRan
7275

7376
private void ConfigurePolicy()
7477
{
75-
retryPolicy = Policy.Handle<HttpRequestException>().Or<TimeoutException>().WaitAndRetryAsync(settings.RetryCount, i => settings.RetryInterval);
78+
this.retryPolicy = Policy.Handle<HttpRequestException>().Or<TimeoutException>().WaitAndRetryAsync(settings.RetryCount, i => settings.RetryInterval);
7679
}
7780
}
7881
}

tests/SendGrid.Tests/Integration.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace SendGrid.Tests
1+
using SendGrid.Tests.Reliability;
2+
3+
namespace SendGrid.Tests
24
{
35
using SendGrid.Helpers.Mail;
46
using Newtonsoft.Json;
@@ -43,7 +45,7 @@ public void Dispose()
4345
{
4446
if (Environment.GetEnvironmentVariable("TRAVIS") != "true")
4547
{
46-
process.Kill();
48+
process.Kill();
4749
Trace.WriteLine("Shutting Down Prism");
4850
}
4951
}
@@ -6061,12 +6063,15 @@ public async Task TestRetryBehaviourThrowsTimeoutException()
60616063
var options = new SendGridClientOptions
60626064
{
60636065
ApiKey = fixture.apiKey,
6064-
ReliabilitySettings = {RetryCount = 2}
6066+
ReliabilitySettings = {RetryCount = 1}
60656067
};
60666068

60676069
var id = "test_url_param";
60686070

6069-
var httpMessageHandler = new TimeOutExceptionThrowingHttpMessageHandler(20, "The operation timed out");
6071+
var httpMessageHandler = new RetryTestBehaviourDelegatingHandler();
6072+
httpMessageHandler.AddBehaviour(httpMessageHandler.TaskCancelled);
6073+
httpMessageHandler.AddBehaviour(httpMessageHandler.TaskCancelled);
6074+
60706075
var retryHandler = new RetryDelegatingHandler(httpMessageHandler, options.ReliabilitySettings);
60716076

60726077
HttpClient clientToInject = new HttpClient(retryHandler);
@@ -6076,6 +6081,37 @@ public async Task TestRetryBehaviourThrowsTimeoutException()
60766081

60776082
Assert.NotNull(exception);
60786083
}
6084+
6085+
[Fact]
6086+
public async Task TestRetryBehaviourSucceedsOnSecondAttempt()
6087+
{
6088+
var msg = new SendGridMessage();
6089+
msg.SetFrom(new EmailAddress("[email protected]"));
6090+
msg.AddTo(new EmailAddress("[email protected]"));
6091+
msg.SetSubject("Hello World from the SendGrid CSharp Library");
6092+
msg.AddContent(MimeType.Html, "HTML content");
6093+
6094+
var options = new SendGridClientOptions
6095+
{
6096+
ApiKey = fixture.apiKey,
6097+
ReliabilitySettings = { RetryCount = 1 }
6098+
};
6099+
6100+
var id = "test_url_param";
6101+
6102+
var httpMessageHandler = new RetryTestBehaviourDelegatingHandler();
6103+
httpMessageHandler.AddBehaviour(httpMessageHandler.TaskCancelled);
6104+
httpMessageHandler.AddBehaviour(httpMessageHandler.OK);
6105+
6106+
var retryHandler = new RetryDelegatingHandler(httpMessageHandler, options.ReliabilitySettings);
6107+
6108+
HttpClient clientToInject = new HttpClient(retryHandler);
6109+
var sg = new SendGridClient(clientToInject, options);
6110+
6111+
var result = await sg.SendEmailAsync(msg);
6112+
6113+
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
6114+
}
60796115
}
60806116

60816117
public class FakeHttpMessageHandler : HttpMessageHandler

tests/SendGrid.Tests/Helpers/Reliability/RetryDelegatingHandlerTests.cs renamed to tests/SendGrid.Tests/Reliability/RetryDelegatingHandlerTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using System;
2-
using System.Net;
3-
using System.Net.Http;
4-
using System.Threading.Tasks;
5-
using SendGrid.Helpers.Reliability;
6-
using Xunit;
7-
8-
namespace SendGrid.Tests.Helpers.Reliability
1+
namespace SendGrid.Tests.Reliability
92
{
3+
using System;
4+
using System.Net;
5+
using System.Net.Http;
6+
using System.Threading.Tasks;
7+
using SendGrid.Helpers.Reliability;
8+
using Xunit;
9+
1010
public class RetryDelegatingHandlerTests
1111
{
1212
private ReliabilitySettings reliabilitySettings;

tests/SendGrid.Tests/Helpers/Reliability/RetryTestBehaviourDelegatingHandler.cs renamed to tests/SendGrid.Tests/Reliability/RetryTestBehaviourDelegatingHandler.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Net;
4-
using System.Net.Http;
5-
using System.Threading;
6-
using System.Threading.Tasks;
7-
8-
namespace SendGrid.Tests.Helpers.Reliability
1+
namespace SendGrid.Tests.Reliability
92
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
1010
public class RetryTestBehaviourDelegatingHandler : DelegatingHandler
1111
{
1212
private readonly List<Func<Task<HttpResponseMessage>>> behaviours;

0 commit comments

Comments
 (0)