Skip to content

Commit 8e4912e

Browse files
authored
Add more validation on RetryOptions (#6702)
1 parent ac526fc commit 8e4912e

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

src/WebJobs.Script/CustomAttributeBuilderUtility.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ internal static CustomAttributeBuilder GetRetryCustomAttributeBuilder(RetryOptio
2020
ConstructorInfo fixedDelayRetryCtorInfo = fixedDelayRetryType.GetConstructor(new[] { typeof(int), typeof(string) });
2121
CustomAttributeBuilder fixedDelayRetryBuilder = new CustomAttributeBuilder(
2222
fixedDelayRetryCtorInfo,
23-
new object[] { functionRetry.MaxRetryCount, functionRetry.DelayInterval.ToString() });
23+
new object[] { functionRetry.MaxRetryCount.Value, functionRetry.DelayInterval.ToString() });
2424
return fixedDelayRetryBuilder;
2525
case RetryStrategy.ExponentialBackoff:
2626
Type exponentialBackoffRetryType = typeof(ExponentialBackoffRetryAttribute);
2727
ConstructorInfo exponentialBackoffDelayRetryCtorInfo = exponentialBackoffRetryType.GetConstructor(new[] { typeof(int), typeof(string), typeof(string) });
2828
CustomAttributeBuilder exponentialBackoffRetryBuilder = new CustomAttributeBuilder(
2929
exponentialBackoffDelayRetryCtorInfo,
30-
new object[] { functionRetry.MaxRetryCount, functionRetry.MinimumInterval.ToString(), functionRetry.MaximumInterval.ToString() });
30+
new object[] { functionRetry.MaxRetryCount.Value, functionRetry.MinimumInterval.ToString(), functionRetry.MaximumInterval.ToString() });
3131
return exponentialBackoffRetryBuilder;
3232
}
3333
return null;

src/WebJobs.Script/Utility.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -767,27 +767,31 @@ public static void ValidateRetryOptions(RetryOptions
767767
{
768768
return;
769769
}
770+
if (!retryOptions.MaxRetryCount.HasValue)
771+
{
772+
throw new ArgumentNullException(nameof(retryOptions.MaxRetryCount));
773+
}
770774
switch (retryOptions.Strategy)
771775
{
772776
case RetryStrategy.FixedDelay:
773-
if (retryOptions.DelayInterval.Ticks <= 0)
777+
if (!retryOptions.DelayInterval.HasValue)
774778
{
775-
throw new ArgumentOutOfRangeException(nameof(retryOptions.DelayInterval));
779+
throw new ArgumentNullException(nameof(retryOptions.DelayInterval));
776780
}
777781
// ensure values specified to create FixedDelayRetryAttribute are valid
778-
_ = new FixedDelayRetryAttribute(retryOptions.MaxRetryCount, retryOptions.DelayInterval.ToString());
782+
_ = new FixedDelayRetryAttribute(retryOptions.MaxRetryCount.Value, retryOptions.DelayInterval.ToString());
779783
break;
780784
case RetryStrategy.ExponentialBackoff:
781-
if (retryOptions.MinimumInterval.Ticks <= 0)
785+
if (!retryOptions.MinimumInterval.HasValue)
782786
{
783-
throw new ArgumentOutOfRangeException(nameof(retryOptions.DelayInterval));
787+
throw new ArgumentNullException(nameof(retryOptions.DelayInterval));
784788
}
785-
if (retryOptions.MaximumInterval.Ticks <= 0)
789+
if (!retryOptions.MaximumInterval.HasValue)
786790
{
787-
throw new ArgumentOutOfRangeException(nameof(retryOptions.DelayInterval));
791+
throw new ArgumentNullException(nameof(retryOptions.DelayInterval));
788792
}
789793
// ensure values specified to create ExponentialBackoffRetryAttribute are valid
790-
_ = new ExponentialBackoffRetryAttribute(retryOptions.MaxRetryCount, retryOptions.MinimumInterval.ToString(), retryOptions.MaximumInterval.ToString());
794+
_ = new ExponentialBackoffRetryAttribute(retryOptions.MaxRetryCount.Value, retryOptions.MinimumInterval.ToString(), retryOptions.MaximumInterval.ToString());
791795
break;
792796
}
793797
}

src/WebJobs.Script/WebJobs.Script.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="4.0.1" />
5151
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.0.8" />
5252
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.22" />
53-
<PackageReference Include="Microsoft.Azure.WebJobs.Script.Abstractions" Version="1.0.1-preview" />
53+
<PackageReference Include="Microsoft.Azure.WebJobs.Script.Abstractions" Version="1.0.2-preview" />
5454
<PackageReference Include="Microsoft.Build" Version="15.8.166" />
5555
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.3.1" />
5656
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="2.1.0" />

test/WebJobs.Script.Tests/Configuration/ScriptHostOptionsSetupTests.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ public void Configure_AppliesTimeout()
148148
[InlineData("fixedDelay", "3", null, true)]
149149
[InlineData("fixedDelay", "3", "-10000000000", true)]
150150
[InlineData("fixedDelay", "3", "10000000000:00000000:40000", true)]
151+
[InlineData("fixedDelay", null, "00:00:05", true)]
152+
[InlineData("fixedDelay", "-1", "00:00:05", false)]
153+
[InlineData("fixedDelay", "-4", "00:00:05", true)]
151154
public void Configure_AppliesRetry_FixedDelay(string expectedStrategy, string maxRetryCount, string delayInterval, bool throwsError)
152155
{
153156
var settings = new Dictionary<string, string>
@@ -156,19 +159,24 @@ public void Configure_AppliesRetry_FixedDelay(string expectedStrategy, string ma
156159
{ ConfigurationPath.Combine(ConfigurationSectionNames.JobHost, "retry", "maxRetryCount"), maxRetryCount },
157160
{ ConfigurationPath.Combine(ConfigurationSectionNames.JobHost, "retry", "delayInterval"), delayInterval }
158161
};
159-
if (string.IsNullOrEmpty(delayInterval))
162+
if (string.IsNullOrEmpty(delayInterval) || string.IsNullOrEmpty(maxRetryCount))
160163
{
161-
Assert.Throws<ArgumentOutOfRangeException>(() => GetConfiguredOptions(settings));
164+
Assert.Throws<ArgumentNullException>(() => GetConfiguredOptions(settings));
162165
return;
163166
}
164167
if (throwsError)
165168
{
169+
if (int.Parse(maxRetryCount) <= 0)
170+
{
171+
Assert.Throws<ArgumentOutOfRangeException>(() => GetConfiguredOptions(settings));
172+
return;
173+
}
166174
Assert.Throws<InvalidOperationException>(() => GetConfiguredOptions(settings));
167175
return;
168176
}
169177
var options = GetConfiguredOptions(settings);
170178
Assert.Equal(RetryStrategy.FixedDelay, options.Retry.Strategy);
171-
Assert.Equal(int.Parse(maxRetryCount), options.Retry.MaxRetryCount);
179+
Assert.Equal(int.Parse(maxRetryCount), options.Retry.MaxRetryCount.Value);
172180
Assert.Equal(TimeSpan.Parse(delayInterval), options.Retry.DelayInterval);
173181
}
174182

@@ -189,7 +197,7 @@ public void Configure_AppliesRetry_ExponentialBackOffDelay(string expectedStrate
189197
};
190198
if (string.IsNullOrEmpty(minimumInterval) || string.IsNullOrEmpty(maximumInterval))
191199
{
192-
Assert.Throws<ArgumentOutOfRangeException>(() => GetConfiguredOptions(settings));
200+
Assert.Throws<ArgumentNullException>(() => GetConfiguredOptions(settings));
193201
return;
194202
}
195203
var minIntervalTimeSpan = TimeSpan.Parse(minimumInterval);

0 commit comments

Comments
 (0)