Skip to content

Commit 0d1c944

Browse files
committed
Defensive checks on ReliabilitySetting
1 parent c2778f8 commit 0d1c944

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

src/SendGrid/Reliability/ReliabilitySettings.cs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,58 @@ namespace SendGrid.Reliability
77
/// </summary>
88
public class ReliabilitySettings
99
{
10+
private int retryCount;
11+
private TimeSpan retryInterval;
12+
1013
/// <summary>
1114
/// Initializes a new instance of the <see cref="ReliabilitySettings"/> class.
1215
/// </summary>
1316
public ReliabilitySettings()
1417
{
15-
RetryCount = 0;
16-
RetryInterval = TimeSpan.FromSeconds(1);
18+
retryCount = 0;
19+
retryInterval = TimeSpan.FromSeconds(1);
1720
}
1821

1922
/// <summary>
2023
/// Gets or sets the number of retries to execute against an HTTP service endpoint before throwing an exceptions. Defaults to 0 (no retries, you must explicitly enable)
2124
/// </summary>
22-
public int RetryCount { get; set; }
25+
public int RetryCount
26+
{
27+
get
28+
{
29+
return retryCount;
30+
}
31+
32+
set
33+
{
34+
if (value < 0)
35+
{
36+
throw new ArgumentException("Retry count must be greater than zero");
37+
}
38+
39+
retryCount = value;
40+
}
41+
}
2342

2443
/// <summary>
2544
/// Gets or sets the interval between HTTP retries. Defaults to 1 second
2645
/// </summary>
27-
public TimeSpan RetryInterval { get; set; }
46+
public TimeSpan RetryInterval
47+
{
48+
get
49+
{
50+
return retryInterval;
51+
}
52+
53+
set
54+
{
55+
if (value.TotalSeconds > 30)
56+
{
57+
throw new ArgumentException("Maximum retry interval is 30 seconds");
58+
}
59+
60+
retryInterval = value;
61+
}
62+
}
2863
}
2964
}

tests/SendGrid.Tests/Reliability/RetryDelegatingHandlerTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,21 @@ await Assert.ThrowsAsync<HttpRequestException>(
9191

9292
Assert.Equal(3, innerHandler.InvocationCount);
9393
}
94+
95+
[Fact]
96+
public void ReliabilitySettingsShouldNotAllowNegativeRetryCount()
97+
{
98+
var settings = new ReliabilitySettings();
99+
100+
Assert.Throws<ArgumentException>(() => settings.RetryCount = -1);
101+
}
102+
103+
[Fact]
104+
public void ReliabilitySettingsShouldNotAllowRetryIntervalGreaterThan30Seconds()
105+
{
106+
var settings = new ReliabilitySettings();
107+
108+
Assert.Throws<ArgumentException>(() => settings.RetryInterval = TimeSpan.FromSeconds(31));
109+
}
94110
}
95111
}

0 commit comments

Comments
 (0)