Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk committed Jun 21, 2023
1 parent 43fba27 commit bd696f3
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions test/Polly.Core.Tests/PredicateBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
using Polly.CircuitBreaker;
using Polly.Fallback;
using Polly.Hedging;
using Polly.Retry;

namespace Polly.Core.Tests;

public class PredicateBuilderTests
Expand All @@ -24,6 +29,13 @@ public class PredicateBuilderTests
{ builder => builder.HandleInner<FormatException>(e => e.Message == "x"), Outcome.FromException<string>(new InvalidOperationException("dummy", new FormatException("m") )), false },
};

[Fact]
public void Ctor_Ok()
{
new PredicateBuilder().Should().NotBeNull();
new PredicateBuilder<string>().Should().NotBeNull();
}

[MemberData(nameof(HandleResultData))]
[Theory]
public void HandleResult_Ok(Action<PredicateBuilder<string>> configure, Outcome<string> value, bool handled)
Expand All @@ -45,4 +57,56 @@ public void CreatePredicate_NotConfigured_Throws()
.Throw<InvalidOperationException>()
.WithMessage("No predicates were configured. There must be at least one predicate added.");
}

[Fact]
public async Task Operator_RetryStrategyOptions_Ok()
{
var options = new RetryStrategyOptions<string>
{
ShouldHandle = new PredicateBuilder<string>().HandleResult("error")
};

var handled = await options.ShouldHandle(new(ResilienceContext.Get(), Outcome.FromResult("error"), new RetryPredicateArguments(0)));

handled.Should().BeTrue();
}

[Fact]
public async Task Operator_FallbackStrategyOptions_Ok()
{
var options = new FallbackStrategyOptions<string>
{
ShouldHandle = new PredicateBuilder<string>().HandleResult("error")
};

var handled = await options.ShouldHandle(new(ResilienceContext.Get(), Outcome.FromResult("error"), new FallbackPredicateArguments()));

handled.Should().BeTrue();
}

[Fact]
public async Task Operator_HedgingStrategyOptions_Ok()
{
var options = new HedgingStrategyOptions<string>
{
ShouldHandle = new PredicateBuilder<string>().HandleResult("error")
};

var handled = await options.ShouldHandle(new(ResilienceContext.Get(), Outcome.FromResult("error"), new HedgingPredicateArguments()));

handled.Should().BeTrue();
}

[Fact]
public async Task Operator_AdvancedCircuitBreakerStrategyOptions_Ok()
{
var options = new AdvancedCircuitBreakerStrategyOptions<string>
{
ShouldHandle = new PredicateBuilder<string>().HandleResult("error")
};

var handled = await options.ShouldHandle(new(ResilienceContext.Get(), Outcome.FromResult("error"), new CircuitBreakerPredicateArguments()));

handled.Should().BeTrue();
}
}

0 comments on commit bd696f3

Please sign in to comment.