public void Should_return_fallback_value_when_execute_delegate_raises_fault_handled_by_policy() { FallbackPolicy <ResultPrimitive> fallbackPolicy = Policy .HandleResult(ResultPrimitive.Fault) .Fallback(ResultPrimitive.Substitute); fallbackPolicy.RaiseResultSequence(ResultPrimitive.Fault).Should().Be(ResultPrimitive.Substitute); }
public void Should_execute_fallback_when_result_raised_matches_handling_predicates() { bool fallbackActionExecuted = false; Func <ResultPrimitive> fallbackAction = () => { fallbackActionExecuted = true; return(ResultPrimitive.Substitute); }; FallbackPolicy <ResultPrimitive> fallbackPolicy = Policy .HandleResult <ResultPrimitive>(r => true) .Fallback(fallbackAction); fallbackPolicy.RaiseResultSequence(ResultPrimitive.Undefined).Should().Be(ResultPrimitive.Substitute); fallbackActionExecuted.Should().BeTrue(); }
public void Should_execute_fallback_when_execute_delegate_raises_fault_handled_by_policy() { bool fallbackActionExecuted = false; Func <ResultPrimitive> fallbackAction = () => { fallbackActionExecuted = true; return(ResultPrimitive.Substitute); }; FallbackPolicy <ResultPrimitive> fallbackPolicy = Policy .HandleResult(ResultPrimitive.Fault) .Fallback(fallbackAction); fallbackPolicy.RaiseResultSequence(ResultPrimitive.Fault).Should().Be(ResultPrimitive.Substitute); fallbackActionExecuted.Should().BeTrue(); }
public void Should_not_execute_fallback_when_execute_delegate_raises_fault_not_handled_by_any_of_predicates() { bool fallbackActionExecuted = false; Func <ResultPrimitive> fallbackAction = () => { fallbackActionExecuted = true; return(ResultPrimitive.Substitute); }; FallbackPolicy <ResultPrimitive> fallbackPolicy = Policy .HandleResult <ResultPrimitive>(r => r == ResultPrimitive.Fault) .OrResult(r => r == ResultPrimitive.FaultAgain) .Fallback(fallbackAction); fallbackPolicy.RaiseResultSequence(ResultPrimitive.FaultYetAgain).Should().Be(ResultPrimitive.FaultYetAgain); fallbackActionExecuted.Should().BeFalse(); }
public void Context_should_be_empty_if_execute_not_called_with_any_context_data() { Context capturedContext = null; bool onFallbackExecuted = false; Func <Context, ResultPrimitive> fallbackAction = _ => ResultPrimitive.Substitute; Action <DelegateResult <ResultPrimitive>, Context> onFallback = (ex, ctx) => { onFallbackExecuted = true; capturedContext = ctx; }; FallbackPolicy <ResultPrimitive> fallbackPolicy = Policy .HandleResult(ResultPrimitive.Fault) .OrResult(ResultPrimitive.FaultAgain) .Fallback(fallbackAction, onFallback); fallbackPolicy.RaiseResultSequence(ResultPrimitive.Fault); onFallbackExecuted.Should().BeTrue(); capturedContext.Should().BeEmpty(); }
public void Should_not_handle_result_raised_by_fallback_delegate_even_if_is_result_handled_by_policy() { bool fallbackActionExecuted = false; Func <ResultClass> fallbackAction = () => { fallbackActionExecuted = true; return(new ResultClass(ResultPrimitive.Fault, "FromFallbackAction")); }; FallbackPolicy <ResultClass> fallbackPolicy = Policy .HandleResult <ResultClass>(r => r.ResultCode == ResultPrimitive.Fault) .Fallback(fallbackAction); fallbackPolicy.RaiseResultSequence(new ResultClass(ResultPrimitive.Fault, "FromExecuteDelegate")) .Should().Match <ResultClass>(r => r.ResultCode == ResultPrimitive.Fault && r.SomeString == "FromFallbackAction"); fallbackActionExecuted.Should().BeTrue(); }
public void Context_should_be_empty_at_fallbackAction_if_execute_not_called_with_any_context_data() { Context capturedContext = null; bool fallbackExecuted = false; Func <Context, CancellationToken, ResultPrimitive> fallbackAction = (ctx, _) => { fallbackExecuted = true; capturedContext = ctx; return(ResultPrimitive.Substitute); }; Action <DelegateResult <ResultPrimitive>, Context> onFallback = (_, _) => { }; FallbackPolicy <ResultPrimitive> fallbackPolicy = Policy .HandleResult(ResultPrimitive.Fault) .OrResult(ResultPrimitive.FaultAgain) .Fallback(fallbackAction, onFallback); fallbackPolicy.RaiseResultSequence(ResultPrimitive.Fault); fallbackExecuted.Should().BeTrue(); capturedContext.Should().BeEmpty(); }