Skip to content

Commit 911be28

Browse files
authored
Fix CA1062 warnings (#2227)
Fix warnings for `TimeoutPolicy`.
1 parent c07cc48 commit 911be28

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

src/Polly/Timeout/TimeoutPolicy.cs

+18-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/// <summary>
44
/// A timeout policy which can be applied to delegates.
55
/// </summary>
6-
#pragma warning disable CA1062 // Validate arguments of public methods
76
public class TimeoutPolicy : Policy, ITimeoutPolicy
87
{
98
private readonly TimeoutStrategy _timeoutStrategy;
@@ -22,14 +21,21 @@ internal TimeoutPolicy(
2221

2322
/// <inheritdoc/>
2423
[DebuggerStepThrough]
25-
protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
26-
TimeoutEngine.Implementation(
24+
protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
25+
{
26+
if (action is null)
27+
{
28+
throw new ArgumentNullException(nameof(action));
29+
}
30+
31+
return TimeoutEngine.Implementation(
2732
action,
2833
context,
2934
_timeoutProvider,
3035
_timeoutStrategy,
3136
_onTimeout,
3237
cancellationToken);
38+
}
3339
}
3440

3541
/// <summary>
@@ -53,12 +59,19 @@ internal TimeoutPolicy(
5359
}
5460

5561
/// <inheritdoc/>
56-
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
57-
TimeoutEngine.Implementation(
62+
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
63+
{
64+
if (action is null)
65+
{
66+
throw new ArgumentNullException(nameof(action));
67+
}
68+
69+
return TimeoutEngine.Implementation(
5870
action,
5971
context,
6072
_timeoutProvider,
6173
_timeoutStrategy,
6274
_onTimeout,
6375
cancellationToken);
76+
}
6477
}

test/Polly.Specs/Timeout/TimeoutSpecs.cs

+28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@ public class TimeoutSpecs : TimeoutSpecsBase
55
{
66
#region Configuration
77

8+
[Fact]
9+
public void Should_throw_when_action_is_null()
10+
{
11+
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
12+
Func<Context, CancellationToken, EmptyStruct> action = null!;
13+
Func<Context, TimeSpan> timeoutProvider = (_) => TimeSpan.Zero;
14+
TimeoutStrategy timeoutStrategy = TimeoutStrategy.Optimistic;
15+
Action<Context, TimeSpan, Task, Exception> onTimeout = (_, _, _, _) => { };
16+
17+
var instance = Activator.CreateInstance(
18+
typeof(TimeoutPolicy),
19+
flags,
20+
null,
21+
[timeoutProvider, timeoutStrategy, onTimeout],
22+
null)!;
23+
var instanceType = instance.GetType();
24+
var methods = instanceType.GetMethods(flags);
25+
var methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "TResult" });
26+
var generic = methodInfo.MakeGenericMethod(typeof(EmptyStruct));
27+
28+
var func = () => generic.Invoke(instance, [action, new Context(), CancellationToken.None]);
29+
30+
var exceptionAssertions = func.Should().Throw<TargetInvocationException>();
31+
exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation.");
32+
exceptionAssertions.And.InnerException.Should().BeOfType<ArgumentNullException>()
33+
.Which.ParamName.Should().Be("action");
34+
}
35+
836
[Fact]
937
public void Should_throw_when_timeout_is_zero_by_timespan()
1038
{

test/Polly.Specs/Timeout/TimeoutTResultSpecs.cs

+27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,33 @@ public class TimeoutTResultSpecs : TimeoutSpecsBase
55
{
66
#region Configuration
77

8+
[Fact]
9+
public void Should_throw_when_action_is_null()
10+
{
11+
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
12+
Func<Context, CancellationToken, EmptyStruct> action = null!;
13+
Func<Context, TimeSpan> timeoutProvider = (_) => TimeSpan.Zero;
14+
TimeoutStrategy timeoutStrategy = TimeoutStrategy.Optimistic;
15+
Action<Context, TimeSpan, Task, Exception> onTimeout = (_, _, _, _) => { };
16+
17+
var instance = Activator.CreateInstance(
18+
typeof(TimeoutPolicy<EmptyStruct>),
19+
flags,
20+
null,
21+
[timeoutProvider, timeoutStrategy, onTimeout],
22+
null)!;
23+
var instanceType = instance.GetType();
24+
var methods = instanceType.GetMethods(flags);
25+
var methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "EmptyStruct" });
26+
27+
var func = () => methodInfo.Invoke(instance, [action, new Context(), CancellationToken.None]);
28+
29+
var exceptionAssertions = func.Should().Throw<TargetInvocationException>();
30+
exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation.");
31+
exceptionAssertions.And.InnerException.Should().BeOfType<ArgumentNullException>()
32+
.Which.ParamName.Should().Be("action");
33+
}
34+
835
[Fact]
936
public void Should_throw_when_timeout_is_zero_by_timespan()
1037
{

0 commit comments

Comments
 (0)