Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warning CA1062#RateLimitPolicy #2222

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/Polly/RateLimit/RateLimitPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Polly.RateLimit;
/// <summary>
/// A rate-limit policy that can be applied to synchronous delegates.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public class RateLimitPolicy : Policy, IRateLimitPolicy
{
private readonly IRateLimiter _rateLimiter;
Expand All @@ -14,8 +13,15 @@ internal RateLimitPolicy(IRateLimiter rateLimiter) =>

/// <inheritdoc/>
[DebuggerStepThrough]
protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
RateLimitEngine.Implementation(_rateLimiter, null, action, context, cancellationToken);
protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}

return RateLimitEngine.Implementation(_rateLimiter, null, action, context, cancellationToken);
}
}

/// <summary>
Expand All @@ -37,6 +43,13 @@ internal RateLimitPolicy(

/// <inheritdoc/>
[DebuggerStepThrough]
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
RateLimitEngine.Implementation(_rateLimiter, _retryAfterFactory, action, context, cancellationToken);
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}

return RateLimitEngine.Implementation(_rateLimiter, _retryAfterFactory, action, context, cancellationToken);
}
}
26 changes: 26 additions & 0 deletions test/Polly.Specs/RateLimit/RateLimitPolicySpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,30 @@ protected override (bool, TimeSpan) TryExecuteThroughPolicy(IRateLimitPolicy pol
throw new InvalidOperationException("Unexpected policy type in test construction.");
}
}

[Fact]
public void Should_throw_when_action_is_null()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
Func<Context, CancellationToken, EmptyStruct> action = null!;
IRateLimiter rateLimiter = RateLimiterFactory.Create(TimeSpan.FromSeconds(1), 1);

var instance = Activator.CreateInstance(
typeof(RateLimitPolicy),
flags,
null,
[rateLimiter],
null)!;
var instanceType = instance.GetType();
var methods = instanceType.GetMethods(flags);
var methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "TResult" });
var generic = methodInfo.MakeGenericMethod(typeof(EmptyStruct));

var func = () => generic.Invoke(instance, [action, new Context(), CancellationToken.None]);

var exceptionAssertions = func.Should().Throw<TargetInvocationException>();
exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation.");
exceptionAssertions.And.InnerException.Should().BeOfType<ArgumentNullException>()
.Which.ParamName.Should().Be("action");
}
}
26 changes: 26 additions & 0 deletions test/Polly.Specs/RateLimit/RateLimitPolicyTResultSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,30 @@ protected override TResult TryExecuteThroughPolicy<TResult>(IRateLimitPolicy<TRe
throw new InvalidOperationException("Unexpected policy type in test construction.");
}
}

[Fact]
public void Should_throw_when_action_is_null()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
Func<Context, CancellationToken, EmptyStruct> action = null!;
IRateLimiter rateLimiter = RateLimiterFactory.Create(TimeSpan.FromSeconds(1), 1);
Func<TimeSpan, Context, EmptyStruct>? retryAfterFactory = null;

var instance = Activator.CreateInstance(
typeof(RateLimitPolicy<EmptyStruct>),
flags,
null,
[rateLimiter, retryAfterFactory],
null)!;
var instanceType = instance.GetType();
var methods = instanceType.GetMethods(flags);
var methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "EmptyStruct" });

var func = () => methodInfo.Invoke(instance, [action, new Context(), CancellationToken.None]);

var exceptionAssertions = func.Should().Throw<TargetInvocationException>();
exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation.");
exceptionAssertions.And.InnerException.Should().BeOfType<ArgumentNullException>()
.Which.ParamName.Should().Be("action");
}
}