Skip to content

Commit 6a4b17d

Browse files
authored
Fix CA1062 warning (#2222)
Fix warnings in `RateLimitPolicy`.
1 parent 4e05ea1 commit 6a4b17d

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

src/Polly/RateLimit/RateLimitPolicy.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ namespace Polly.RateLimit;
44
/// <summary>
55
/// A rate-limit policy that can be applied to synchronous delegates.
66
/// </summary>
7-
#pragma warning disable CA1062 // Validate arguments of public methods
87
public class RateLimitPolicy : Policy, IRateLimitPolicy
98
{
109
private readonly IRateLimiter _rateLimiter;
@@ -14,8 +13,15 @@ internal RateLimitPolicy(IRateLimiter rateLimiter) =>
1413

1514
/// <inheritdoc/>
1615
[DebuggerStepThrough]
17-
protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
18-
RateLimitEngine.Implementation(_rateLimiter, null, action, context, cancellationToken);
16+
protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
17+
{
18+
if (action is null)
19+
{
20+
throw new ArgumentNullException(nameof(action));
21+
}
22+
23+
return RateLimitEngine.Implementation(_rateLimiter, null, action, context, cancellationToken);
24+
}
1925
}
2026

2127
/// <summary>
@@ -37,6 +43,13 @@ internal RateLimitPolicy(
3743

3844
/// <inheritdoc/>
3945
[DebuggerStepThrough]
40-
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
41-
RateLimitEngine.Implementation(_rateLimiter, _retryAfterFactory, action, context, cancellationToken);
46+
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
47+
{
48+
if (action is null)
49+
{
50+
throw new ArgumentNullException(nameof(action));
51+
}
52+
53+
return RateLimitEngine.Implementation(_rateLimiter, _retryAfterFactory, action, context, cancellationToken);
54+
}
4255
}

test/Polly.Specs/RateLimit/RateLimitPolicySpecs.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,30 @@ protected override (bool, TimeSpan) TryExecuteThroughPolicy(IRateLimitPolicy pol
3131
throw new InvalidOperationException("Unexpected policy type in test construction.");
3232
}
3333
}
34+
35+
[Fact]
36+
public void Should_throw_when_action_is_null()
37+
{
38+
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
39+
Func<Context, CancellationToken, EmptyStruct> action = null!;
40+
IRateLimiter rateLimiter = RateLimiterFactory.Create(TimeSpan.FromSeconds(1), 1);
41+
42+
var instance = Activator.CreateInstance(
43+
typeof(RateLimitPolicy),
44+
flags,
45+
null,
46+
[rateLimiter],
47+
null)!;
48+
var instanceType = instance.GetType();
49+
var methods = instanceType.GetMethods(flags);
50+
var methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "TResult" });
51+
var generic = methodInfo.MakeGenericMethod(typeof(EmptyStruct));
52+
53+
var func = () => generic.Invoke(instance, [action, new Context(), CancellationToken.None]);
54+
55+
var exceptionAssertions = func.Should().Throw<TargetInvocationException>();
56+
exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation.");
57+
exceptionAssertions.And.InnerException.Should().BeOfType<ArgumentNullException>()
58+
.Which.ParamName.Should().Be("action");
59+
}
3460
}

test/Polly.Specs/RateLimit/RateLimitPolicyTResultSpecs.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,30 @@ protected override TResult TryExecuteThroughPolicy<TResult>(IRateLimitPolicy<TRe
4747
throw new InvalidOperationException("Unexpected policy type in test construction.");
4848
}
4949
}
50+
51+
[Fact]
52+
public void Should_throw_when_action_is_null()
53+
{
54+
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
55+
Func<Context, CancellationToken, EmptyStruct> action = null!;
56+
IRateLimiter rateLimiter = RateLimiterFactory.Create(TimeSpan.FromSeconds(1), 1);
57+
Func<TimeSpan, Context, EmptyStruct>? retryAfterFactory = null;
58+
59+
var instance = Activator.CreateInstance(
60+
typeof(RateLimitPolicy<EmptyStruct>),
61+
flags,
62+
null,
63+
[rateLimiter, retryAfterFactory],
64+
null)!;
65+
var instanceType = instance.GetType();
66+
var methods = instanceType.GetMethods(flags);
67+
var methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "EmptyStruct" });
68+
69+
var func = () => methodInfo.Invoke(instance, [action, new Context(), CancellationToken.None]);
70+
71+
var exceptionAssertions = func.Should().Throw<TargetInvocationException>();
72+
exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation.");
73+
exceptionAssertions.And.InnerException.Should().BeOfType<ArgumentNullException>()
74+
.Which.ParamName.Should().Be("action");
75+
}
5076
}

0 commit comments

Comments
 (0)