Skip to content
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/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