Skip to content

Commit

Permalink
Improve unit test coverage in Polly.Specs (#1974)
Browse files Browse the repository at this point in the history
  • Loading branch information
gintsk authored Feb 19, 2024
1 parent a6b9b33 commit 1ce60b2
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/Polly.Specs/PolicyContextAndKeyAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ public void Should_not_be_able_to_configure_the_policy_key_explicitly_after_retr
configure.Should().Throw<ArgumentException>().And.ParamName.Should().Be("policyKey");
}

[Fact]
public void Should_throw_when_onretry_is_null()
{
var policyBuilder = Policy.Handle<Exception>();
Assert.Throws<ArgumentNullException>("onRetry", () => policyBuilder.RetryAsync(3, default(Action<Exception, int, Context>)));
}

#endregion

#region PolicyKey and execution Context tests
Expand Down
38 changes: 38 additions & 0 deletions test/Polly.Specs/Retry/RetryForeverAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,44 @@ await policy.Awaiting(x => x.RaiseExceptionAsync<ArgumentException>())
.Should().NotThrowAsync();
}

[Fact]
public void Should_throw_when_onretry_exception_is_null()
{
var policyBuilder = Policy.Handle<Exception>();
Assert.Throws<ArgumentNullException>("onRetry", () => policyBuilder.RetryForeverAsync(default(Action<Exception>)));
}

[Fact]
public void Should_throw_when_onretry_exception_int_is_null()
{
var policyBuilder = Policy.Handle<Exception>();
Assert.Throws<ArgumentNullException>("onRetry", () => policyBuilder.RetryForeverAsync(default(Action<Exception, int>)));
}

[Fact]
public void Should_throw_when_onretry_exception_context_is_null()
{
var policyBuilder = Policy.Handle<Exception>();
Assert.Throws<ArgumentNullException>("onRetry", () => policyBuilder.RetryForeverAsync(default(Action<Exception, Context>)));
}

[Fact]
public void Should_throw_when_onretry_exception_int_context_is_null()
{
var policyBuilder = Policy.Handle<Exception>();
Assert.Throws<ArgumentNullException>("onRetry", () => policyBuilder.RetryForeverAsync(default(Action<Exception, int, Context>)));
}

[Fact]
public void Should_throw_when_onretry_exception_timespan_context_is_null()
{
var policyBuilder = Policy.Handle<Exception>();
Assert.Throws<ArgumentNullException>("onRetry", () => policyBuilder.WaitAndRetryAsync(
retryCount: 3,
sleepDurationProvider: _ => TimeSpan.FromSeconds(1),
onRetry: default(Action<Exception, TimeSpan, Context>)));
}

[Fact]
public async Task Should_call_onretry_on_each_retry_with_the_current_exception()
{
Expand Down
61 changes: 61 additions & 0 deletions test/Polly.Specs/Retry/WaitAndRetryAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,67 @@ public void Should_throw_when_onretry_action_is_null_without_context()
.ParamName.Should().Be("onRetry");
}

[Fact]
public void Should_throw_when_onretry_exception_timespan_context_is_null_with_sleep_durations()
{
Action policy = () => Policy
.Handle<DivideByZeroException>()
.WaitAndRetryAsync(Enumerable.Empty<TimeSpan>(), default(Action<Exception, TimeSpan, Context>));

policy.Should().Throw<ArgumentNullException>().And
.ParamName.Should().Be("onRetry");
}

[Fact]
public void Should_throw_when_onretry_exception_timespan_int_context_is_null_with_sleep_duration_provider_int_timespan()
{
Func<int, TimeSpan> provider = _ => TimeSpan.Zero;

Action policy = () => Policy
.Handle<DivideByZeroException>()
.WaitAndRetryAsync(3, provider, default(Action<Exception, TimeSpan, int, Context>));

policy.Should().Throw<ArgumentNullException>().And
.ParamName.Should().Be("onRetry");
}

[Fact]
public void Should_throw_when_onretry_exception_timespan_int_context_is_null_with_sleep_durations()
{
Action policy = () => Policy
.Handle<DivideByZeroException>()
.WaitAndRetryAsync(Enumerable.Empty<TimeSpan>(), default(Action<Exception, TimeSpan, int, Context>));

policy.Should().Throw<ArgumentNullException>().And
.ParamName.Should().Be("onRetry");
}

[Fact]
public void Should_throw_when_onretry_exception_timespan_context_is_null_with_sleep_duration_provider()
{
Func<int, Context, TimeSpan> provider = (_, _) => 1.Seconds();

Action policy = () => Policy
.Handle<DivideByZeroException>()
.WaitAndRetryAsync(3, provider, default(Action<Exception, TimeSpan, Context>));

policy.Should().Throw<ArgumentNullException>().And
.ParamName.Should().Be("onRetry");
}

[Fact]
public void Should_throw_when_onretry_exception_timespan_int_context_is_null_with_sleep_duration_provider()
{
Func<int, Context, TimeSpan> provider = (_, _) => 1.Seconds();

Action policy = () => Policy
.Handle<DivideByZeroException>()
.WaitAndRetryAsync(3, provider, default(Action<Exception, TimeSpan, int, Context>));

policy.Should().Throw<ArgumentNullException>().And
.ParamName.Should().Be("onRetry");
}

[Fact]
public async Task Should_not_throw_when_specified_exception_thrown_same_number_of_times_as_there_are_sleep_durations()
{
Expand Down
72 changes: 72 additions & 0 deletions test/Polly.Specs/Retry/WaitAndRetryForeverAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,78 @@ public void Should_throw_when_sleep_duration_provider_is_null_with_context()
.ParamName.Should().Be("sleepDurationProvider");
}

[Fact]
public void Should_throw_when_sleep_duration_provider_int_timespan_is_null()
{
Action policy = () => Policy
.Handle<Exception>()
.WaitAndRetryForeverAsync(default(Func<int, TimeSpan>));

policy.Should().Throw<ArgumentNullException>().And
.ParamName.Should().Be("sleepDurationProvider");
}

[Fact]
public void Should_throw_when_sleep_duration_provider_int_timespan_is_null_with_onretry()
{
Action<Exception, int, TimeSpan> onRetry = (_, _, _) => { };

Action policy = () => Policy
.Handle<Exception>()
.WaitAndRetryForeverAsync(default, onRetry);

policy.Should().Throw<ArgumentNullException>().And
.ParamName.Should().Be("sleepDurationProvider");
}

[Fact]
public void Should_throw_when_sleep_duration_provider_int_context_timespan_is_null()
{
Action policy = () => Policy
.Handle<Exception>()
.WaitAndRetryForeverAsync(default(Func<int, Context, TimeSpan>));

policy.Should().Throw<ArgumentNullException>().And
.ParamName.Should().Be("sleepDurationProvider");
}

[Fact]
public void Should_throw_when_onretry_exception_int_timespan_is_null_with_sleep_duration_provider()
{
Func<int, TimeSpan> provider = _ => TimeSpan.Zero;

Action policy = () => Policy
.Handle<Exception>()
.WaitAndRetryForeverAsync(provider, default(Action<Exception, int, TimeSpan>));

policy.Should().Throw<ArgumentNullException>().And
.ParamName.Should().Be("onRetry");
}

[Fact]
public void Should_throw_when_sleep_duration_provider_int_context_timespan_is_null_with_retry()
{
Action policy = () => Policy
.Handle<Exception>()
.WaitAndRetryForeverAsync(default(Func<int, Context, TimeSpan>), default(Action<Exception, int, TimeSpan, Context>));

policy.Should().Throw<ArgumentNullException>().And
.ParamName.Should().Be("sleepDurationProvider");
}

[Fact]
public void Should_throw_when_onretry_exception_int_timespan_context_is_null_with_sleep_duration_provider()
{
Func<int, Context, TimeSpan> provider = (_, _) => 1.Seconds();

Action policy = () => Policy
.Handle<Exception>()
.WaitAndRetryForeverAsync(provider, default(Action<Exception, int, TimeSpan, Context>));

policy.Should().Throw<ArgumentNullException>().And
.ParamName.Should().Be("onRetry");
}

[Fact]
public void Should_throw_when_onretry_action_is_null_without_context()
{
Expand Down

0 comments on commit 1ce60b2

Please sign in to comment.