Skip to content

Commit

Permalink
Fix CA1062 warnings (#2227)
Browse files Browse the repository at this point in the history
Fix warnings for `TimeoutPolicy`.
  • Loading branch information
Zombach authored Jul 22, 2024
1 parent c07cc48 commit 911be28
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Polly/Timeout/TimeoutPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// <summary>
/// A timeout policy which can be applied to delegates.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public class TimeoutPolicy : Policy, ITimeoutPolicy
{
private readonly TimeoutStrategy _timeoutStrategy;
Expand All @@ -22,14 +21,21 @@ internal TimeoutPolicy(

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

return TimeoutEngine.Implementation(
action,
context,
_timeoutProvider,
_timeoutStrategy,
_onTimeout,
cancellationToken);
}
}

/// <summary>
Expand All @@ -53,12 +59,19 @@ internal TimeoutPolicy(
}

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

return TimeoutEngine.Implementation(
action,
context,
_timeoutProvider,
_timeoutStrategy,
_onTimeout,
cancellationToken);
}
}
28 changes: 28 additions & 0 deletions test/Polly.Specs/Timeout/TimeoutSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,34 @@ public class TimeoutSpecs : TimeoutSpecsBase
{
#region Configuration

[Fact]
public void Should_throw_when_action_is_null()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
Func<Context, CancellationToken, EmptyStruct> action = null!;
Func<Context, TimeSpan> timeoutProvider = (_) => TimeSpan.Zero;
TimeoutStrategy timeoutStrategy = TimeoutStrategy.Optimistic;
Action<Context, TimeSpan, Task, Exception> onTimeout = (_, _, _, _) => { };

var instance = Activator.CreateInstance(
typeof(TimeoutPolicy),
flags,
null,
[timeoutProvider, timeoutStrategy, onTimeout],
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");
}

[Fact]
public void Should_throw_when_timeout_is_zero_by_timespan()
{
Expand Down
27 changes: 27 additions & 0 deletions test/Polly.Specs/Timeout/TimeoutTResultSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,33 @@ public class TimeoutTResultSpecs : TimeoutSpecsBase
{
#region Configuration

[Fact]
public void Should_throw_when_action_is_null()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
Func<Context, CancellationToken, EmptyStruct> action = null!;
Func<Context, TimeSpan> timeoutProvider = (_) => TimeSpan.Zero;
TimeoutStrategy timeoutStrategy = TimeoutStrategy.Optimistic;
Action<Context, TimeSpan, Task, Exception> onTimeout = (_, _, _, _) => { };

var instance = Activator.CreateInstance(
typeof(TimeoutPolicy<EmptyStruct>),
flags,
null,
[timeoutProvider, timeoutStrategy, onTimeout],
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");
}

[Fact]
public void Should_throw_when_timeout_is_zero_by_timespan()
{
Expand Down

0 comments on commit 911be28

Please sign in to comment.