public void Should_be_able_to_construct_reactive_policy() { Action construct = () => { AsyncAddBehaviourIfHandlePolicy policy = Policy.Handle <Exception>().WithBehaviourAsync(async ex => { // Placeholder for more substantive async work. Console.WriteLine("Handling " + ex.Message); await Task.CompletedTask; }); }; construct.ShouldNotThrow(); }
public void Should_be_able_to_construct_reactive_policy() { Action construct = () => { AsyncAddBehaviourIfHandlePolicy <ResultPrimitive> policy = Policy.HandleResult <ResultPrimitive>(ResultPrimitive.Fault).WithBehaviourAsync(async outcome => { // Placeholder for more substantive async work. Console.WriteLine("Handling " + outcome.Result); await Task.CompletedTask; }); }; construct.Should().NotThrow(); }
public void Reactive_policy_should_be_able_to_ignore_unhandled_exception() { Exception handled = null; AsyncAddBehaviourIfHandlePolicy policy = Policy.Handle <InvalidOperationException>().WithBehaviourAsync(async ex => { handled = ex; await Task.CompletedTask; }); Exception toThrow = new NotImplementedException(); bool executed = false; policy.Awaiting(x => x.ExecuteAsync(() => { executed = true; throw toThrow; })) .ShouldThrow <Exception>().Which.Should().Be(toThrow); executed.Should().BeTrue(); handled.Should().Be(null); }
public async Task Reactive_policy_should_be_able_to_ignore_unhandled_result() { ResultPrimitive?handled = null; AsyncAddBehaviourIfHandlePolicy <ResultPrimitive> policy = Policy .HandleResult <ResultPrimitive>(ResultPrimitive.Fault) .WithBehaviourAsync(async outcome => { handled = outcome.Result; await Task.CompletedTask; }); ResultPrimitive toReturn = ResultPrimitive.FaultYetAgain; bool executed = false; (await policy.ExecuteAsync(async() => { executed = true; await Task.CompletedTask; return(toReturn); })) .Should().Be(toReturn); executed.Should().BeTrue(); handled.Should().Be(null); }
public async Task Reactive_policy_should_handle_result() { ResultPrimitive handled = ResultPrimitive.Undefined; AsyncAddBehaviourIfHandlePolicy <ResultPrimitive> policy = Policy .HandleResult <ResultPrimitive>(ResultPrimitive.Fault) .WithBehaviourAsync(async outcome => { handled = outcome.Result; await Task.CompletedTask; }); ResultPrimitive toReturn = ResultPrimitive.Fault; bool executed = false; (await policy.ExecuteAsync(async() => { executed = true; await Task.CompletedTask; return(toReturn); })) .Should().Be(toReturn); executed.Should().BeTrue(); handled.Should().Be(toReturn); }