Beispiel #1
0
        public void Should_throw_for_generic_method_execution_on_non_generic_policy()
        {
            FallbackPolicy fallbackPolicy = Policy
                                            .Handle <DivideByZeroException>()
                                            .FallbackAsync(_ => TaskHelper.EmptyTask);

            fallbackPolicy.Awaiting(p => p.ExecuteAsync <int>(() => TaskHelper.FromResult(0))).ShouldThrow <InvalidOperationException>();
        }
Beispiel #2
0
        public void Should_execute_fallback_when_exception_thrown_matches_handling_predicates()
        {
            bool fallbackActionExecuted = false;
            Func <CancellationToken, Task> fallbackActionAsync = _ => { fallbackActionExecuted = true; return(TaskHelper.EmptyTask); };

            FallbackPolicy fallbackPolicy = Policy
                                            .Handle <DivideByZeroException>(e => true)
                                            .FallbackAsync(fallbackActionAsync);

            fallbackPolicy.Awaiting(async x => await x.RaiseExceptionAsync <DivideByZeroException>()).ShouldNotThrow();

            fallbackActionExecuted.Should().BeTrue();
        }
Beispiel #3
0
        public void Should_not_execute_fallback_when_execute_delegate_throws_exception_not_handled_by_policy()
        {
            bool fallbackActionExecuted = false;
            Func <CancellationToken, Task> fallbackActionAsync = _ => { fallbackActionExecuted = true; return(TaskHelper.EmptyTask); };

            FallbackPolicy fallbackPolicy = Policy
                                            .Handle <DivideByZeroException>()
                                            .FallbackAsync(fallbackActionAsync);

            fallbackPolicy.Awaiting(async x => await x.RaiseExceptionAsync <ArgumentNullException>()).ShouldThrow <ArgumentNullException>();

            fallbackActionExecuted.Should().BeFalse();
        }
Beispiel #4
0
        public void Should_not_call_fallbackAction_with_the_exception_if_exception_unhandled()
        {
            Exception fallbackException = null;

            Func <Exception, Context, CancellationToken, Task> fallbackFunc = (ex, ctx, ct) => { fallbackException = ex; return(TaskHelper.EmptyTask); };

            Func <Exception, Context, Task> onFallback = (ex, ctx) => { return(TaskHelper.EmptyTask); };

            FallbackPolicy fallbackPolicy = Policy
                                            .Handle <DivideByZeroException>()
                                            .FallbackAsync(fallbackFunc, onFallback);

            fallbackPolicy.Awaiting(async p => await p.ExecuteAsync(() => { throw new ArgumentNullException(); }))
            .ShouldThrow <ArgumentNullException>();

            fallbackException.Should().BeNull();
        }
Beispiel #5
0
        public void Should_call_fallbackAction_with_the_exception_when_execute_and_capture()
        {
            Exception fallbackException = null;

            Func <Exception, Context, CancellationToken, Task> fallbackFunc = (ex, ctx, ct) => { fallbackException = ex; return(TaskHelper.EmptyTask); };

            Func <Exception, Context, Task> onFallback = (ex, ctx) => { return(TaskHelper.EmptyTask); };

            FallbackPolicy fallbackPolicy = Policy
                                            .Handle <ArgumentNullException>()
                                            .FallbackAsync(fallbackFunc, onFallback);

            fallbackPolicy.Awaiting(async p => await p.ExecuteAndCaptureAsync(() => { throw new ArgumentNullException(); }))
            .ShouldNotThrow();

            fallbackException.Should().NotBeNull()
            .And.BeOfType(typeof(ArgumentNullException));
        }
Beispiel #6
0
        public void Should_call_fallbackAction_with_the_exception()
        {
            Exception fallbackException = null;

            Func <Exception, Context, CancellationToken, Task> fallbackFunc = (ex, ctx, ct) => { fallbackException = ex; return(TaskHelper.EmptyTask); };

            Func <Exception, Context, Task> onFallback = (ex, ctx) => { return(TaskHelper.EmptyTask); };

            FallbackPolicy fallbackPolicy = Policy
                                            .Handle <ArgumentNullException>()
                                            .FallbackAsync(fallbackFunc, onFallback);

            Exception instanceToThrow = new ArgumentNullException("myParam");

            fallbackPolicy.Awaiting(p => p.RaiseExceptionAsync(instanceToThrow))
            .ShouldNotThrow();

            fallbackException.Should().Be(instanceToThrow);
        }
        public void Should_call_fallbackAction_with_the_passed_context_when_execute_and_capture()
        {
            IDictionary <string, object> contextData = null;

            Func <Context, CancellationToken, Task <ResultPrimitive> > fallbackActionAsync = (ctx, ct) => { contextData = ctx; return(Task.FromResult(ResultPrimitive.Substitute)); };

            Func <Exception, Context, Task> onFallbackAsync = (ex, ctx) => TaskHelper.EmptyTask;

            FallbackPolicy fallbackPolicy = Policy
                                            .Handle <ArgumentNullException>()
                                            .FallbackAsync(fallbackActionAsync, onFallbackAsync);

            fallbackPolicy.Awaiting(async p => await p.ExecuteAndCaptureAsync(ctx => { throw new ArgumentNullException(); },
                                                                              new { key1 = "value1", key2 = "value2" }.AsDictionary()))
            .ShouldNotThrow();

            contextData.Should()
            .ContainKeys("key1", "key2").And
            .ContainValues("value1", "value2");
        }
Beispiel #8
0
        public void Should_call_onFallback_with_the_passed_context()
        {
            Func <Context, CancellationToken, Task> fallbackActionAsync = (_, __) => TaskHelper.EmptyTask;

            IDictionary <string, object> contextData = null;

            Func <Exception, Context, Task> onFallbackAsync = (ex, ctx) => { contextData = ctx; return(TaskHelper.EmptyTask); };

            FallbackPolicy fallbackPolicy = Policy
                                            .Handle <ArgumentNullException>()
                                            .FallbackAsync(fallbackActionAsync, onFallbackAsync);

            fallbackPolicy.Awaiting(async p => await p.ExecuteAsync(() => { throw new ArgumentNullException(); },
                                                                    new { key1 = "value1", key2 = "value2" }.AsDictionary()))
            .ShouldNotThrow();

            contextData.Should()
            .ContainKeys("key1", "key2").And
            .ContainValues("value1", "value2");
        }
Beispiel #9
0
        public void Should_not_handle_exception_thrown_by_fallback_delegate_even_if_is_exception_handled_by_policy()
        {
            bool fallbackActionExecuted = false;
            Func <CancellationToken, Task> fallbackActionAsync = _ =>
            {
                fallbackActionExecuted = true;
                throw new DivideByZeroException()
                      {
                          HelpLink = "FromFallbackAction"
                      };
            };

            FallbackPolicy fallbackPolicy = Policy
                                            .Handle <DivideByZeroException>()
                                            .FallbackAsync(fallbackActionAsync);

            fallbackPolicy.Awaiting(async x => await x.RaiseExceptionAsync <DivideByZeroException>((e, i) => e.HelpLink = "FromExecuteDelegate"))
            .ShouldThrow <DivideByZeroException>().And.HelpLink.Should().Be("FromFallbackAction");

            fallbackActionExecuted.Should().BeTrue();
        }