Skip to content

Commit

Permalink
Improve unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
baranyaimate committed Feb 15, 2024
1 parent 3b954f1 commit 540e308
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 5 deletions.
12 changes: 11 additions & 1 deletion test/Polly.Specs/Bulkhead/BulkheadAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ public class BulkheadAsyncSpecs(ITestOutputHelper testOutputHelper) : BulkheadSp
#region Configuration

[Fact]
public void Should_throw_when_maxparallelization_less_or_equal_to_zero()
public void Should_throw_when_maxParallelization_less_or_equal_to_zero_and_no_maxQueuingActions()
{
Action policy = () => Policy
.BulkheadAsync(0);

policy.Should().Throw<ArgumentOutOfRangeException>().And
.ParamName.Should().Be("maxParallelization");
}

[Fact]
public void Should_throw_when_maxParallelization_less_or_equal_to_zero()
{
Action policy = () => Policy
.BulkheadAsync(0, 1);
Expand Down
14 changes: 12 additions & 2 deletions test/Polly.Specs/Bulkhead/BulkheadSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ public BulkheadSpecs(ITestOutputHelper testOutputHelper)
#region Configuration

[Fact]
public void Should_throw_when_maxparallelization_less_or_equal_to_zero()
public void Should_throw_when_maxParallelization_less_or_equal_to_zero_and_no_maxQueuingActions()
{
Action policy = () => Policy
.Bulkhead(0);

policy.Should().Throw<ArgumentOutOfRangeException>().And
.ParamName.Should().Be("maxParallelization");
}

[Fact]
public void Should_throw_when_maxParallelization_less_or_equal_to_zero()
{
Action policy = () => Policy
.Bulkhead(0, 1);
Expand All @@ -21,7 +31,7 @@ public void Should_throw_when_maxparallelization_less_or_equal_to_zero()
}

[Fact]
public void Should_throw_when_maxqueuedactions_less_than_zero()
public void Should_throw_when_maxQueuedActions_less_than_zero()
{
Action policy = () => Policy
.Bulkhead(1, -1);
Expand Down
12 changes: 11 additions & 1 deletion test/Polly.Specs/Bulkhead/BulkheadTResultAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ public BulkheadTResultAsyncSpecs(ITestOutputHelper testOutputHelper)
#region Configuration

[Fact]
public void Should_throw_when_maxparallelization_less_or_equal_to_zero()
public void Should_throw_when_maxParallelization_less_or_equal_to_zero_and_no_maxQueuingActions()
{
Action policy = () => Policy
.BulkheadAsync(0);

policy.Should().Throw<ArgumentOutOfRangeException>().And
.ParamName.Should().Be("maxParallelization");
}

[Fact]
public void Should_throw_when_maxParallelization_less_or_equal_to_zero()
{
Action policy = () => Policy
.BulkheadAsync<int>(0, 1);
Expand Down
12 changes: 11 additions & 1 deletion test/Polly.Specs/Bulkhead/BulkheadTResultSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ public BulkheadTResultSpecs(ITestOutputHelper testOutputHelper)
#region Configuration

[Fact]
public void Should_throw_when_maxparallelization_less_or_equal_to_zero()
public void Should_throw_when_maxParallelization_less_or_equal_to_zero_and_no_maxQueuingActions()
{
Action policy = () => Policy
.Bulkhead<int>(0);

policy.Should().Throw<ArgumentOutOfRangeException>().And
.ParamName.Should().Be("maxParallelization");
}

[Fact]
public void Should_throw_when_maxParallelization_less_or_equal_to_zero()
{
Action policy = () => Policy
.Bulkhead<int>(0, 1);
Expand Down
60 changes: 60 additions & 0 deletions test/Polly.Specs/Caching/ContextualTtlSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,64 @@ public void Should_return_negative_value_set_on_context()
gotTtl.Timespan.Should().Be(ttl);
gotTtl.SlidingExpiration.Should().BeFalse();
}

[Fact]
public void Should_return_zero_if_non_timespan_value_set_on_context()
{
var contextData = new Dictionary<string, object>
{
[ContextualTtl.TimeSpanKey] = "non-timespan value"
};

Context context = new Context(string.Empty, contextData);
new ContextualTtl().GetTtl(context, null).Timespan.Should().Be(TimeSpan.Zero);
}

[Fact]
public void Should_return_sliding_expiration_if_set_on_context()
{
var ttl = TimeSpan.FromSeconds(30);
var contextData = new Dictionary<string, object>
{
[ContextualTtl.TimeSpanKey] = ttl,
[ContextualTtl.SlidingExpirationKey] = true
};

var context = new Context(string.Empty, contextData);
var gotTtl = new ContextualTtl().GetTtl(context, null);
gotTtl.Timespan.Should().Be(ttl);
gotTtl.SlidingExpiration.Should().BeTrue();
}

[Fact]
public void Should_return_no_sliding_expiration_if_set_to_false_on_context()
{
var ttl = TimeSpan.FromSeconds(30);
var contextData = new Dictionary<string, object>
{
[ContextualTtl.TimeSpanKey] = ttl,
[ContextualTtl.SlidingExpirationKey] = false
};

var context = new Context(string.Empty, contextData);
var gotTtl = new ContextualTtl().GetTtl(context, null);
gotTtl.Timespan.Should().Be(ttl);
gotTtl.SlidingExpiration.Should().BeFalse();
}

[Fact]
public void Should_return_no_sliding_expiration_if_non_boolean_value_set_on_context()
{
var ttl = TimeSpan.FromSeconds(30);
var contextData = new Dictionary<string, object>
{
[ContextualTtl.TimeSpanKey] = ttl,
[ContextualTtl.SlidingExpirationKey] = "non-boolean value"
};

var context = new Context(string.Empty, contextData);
var gotTtl = new ContextualTtl().GetTtl(context, null);
gotTtl.Timespan.Should().Be(ttl);
gotTtl.SlidingExpiration.Should().BeFalse();
}
}
61 changes: 61 additions & 0 deletions test/Polly.Specs/Utilities/SystemClockSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace Polly.Specs.Utilities;

public class SystemClockSpecs
{
[Fact]
public void Sleep_ShouldNotThrow_WhenCancellationNotRequested() =>
SystemClock.Sleep.Invoking(s =>
{
using var cts = new CancellationTokenSource();
s(TimeSpan.FromMilliseconds(1), cts.Token);
}).Should().NotThrow();

[Fact]
public void Sleep_ShouldThrow_WhenCancellationRequested() =>
SystemClock.Sleep.Invoking(s =>
{
using var cts = new CancellationTokenSource();
cts.Cancel();
s(TimeSpan.FromMilliseconds(1), cts.Token);
}).Should().Throw<OperationCanceledException>();

[Fact]
public async Task SleepAsync_ShouldNotThrow_WhenCancellationNotRequested() =>
await SystemClock.SleepAsync.Invoking(async s =>
{
using var cts = new CancellationTokenSource();
await s(TimeSpan.FromMilliseconds(1), cts.Token);
}).Should().NotThrowAsync();

[Fact]
public async Task SleepAsync_ShouldThrow_WhenCancellationRequested() =>
await SystemClock.SleepAsync.Invoking(async s =>
{
using var cts = new CancellationTokenSource();
cts.Cancel();
await s(TimeSpan.FromMilliseconds(1), cts.Token);
}).Should().ThrowAsync<OperationCanceledException>();

[Fact]
public void Reset_ShouldResetToDefaultImplementations()
{
SystemClock.Sleep = (_, _) => { };
SystemClock.SleepAsync = (_, _) => Task.CompletedTask;
SystemClock.UtcNow = () => new DateTime(2000, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
SystemClock.DateTimeOffsetUtcNow = () => new DateTimeOffset(new DateTime(2000, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
SystemClock.CancelTokenAfter = (_, _) => { };

SystemClock.Reset();

SystemClock.UtcNow().Should().NotBe(new DateTime(2000, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
SystemClock.DateTimeOffsetUtcNow().Should().NotBe(new DateTimeOffset(new DateTime(2000, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)));

SystemClock.CancelTokenAfter.Invoking(s =>
{
using var cts = new CancellationTokenSource();
s(cts, TimeSpan.FromMilliseconds(1));
Task.Delay(10).Wait();
cts.Token.IsCancellationRequested.Should().BeTrue();
}).Should().NotThrow();
}
}

0 comments on commit 540e308

Please sign in to comment.