Beispiel #1
0
        public async Task Context_should_be_empty_if_execute_not_called_with_any_context_data()
        {
            Context capturedContext    = null;
            bool    onFallbackExecuted = false;

            Func <Context, CancellationToken, Task> fallbackActionAsync = (_, __) => TaskHelper.EmptyTask;
            Func <Exception, Context, Task>         onFallbackAsync     = (ex, ctx) => { onFallbackExecuted = true; capturedContext = ctx; return(TaskHelper.EmptyTask); };

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

            await fallbackPolicy.RaiseExceptionAsync <DivideByZeroException>();

            onFallbackExecuted.Should().BeTrue();
            capturedContext.Should().BeEmpty();
        }
Beispiel #2
0
        public async Task Should_call_onFallback_passing_exception_triggering_fallback()
        {
            bool fallbackActionExecuted = false;
            Func <CancellationToken, Task> fallbackActionAsync = _ => { fallbackActionExecuted = true; return(TaskHelper.EmptyTask); };

            Exception exceptionPassedToOnFallback  = null;
            Func <Exception, Task> onFallbackAsync = ex => { exceptionPassedToOnFallback = ex; return(TaskHelper.EmptyTask); };

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

            Exception instanceToThrow = new ArgumentNullException("myParam");
            await fallbackPolicy.RaiseExceptionAsync(instanceToThrow);

            fallbackActionExecuted.Should().BeTrue();
            exceptionPassedToOnFallback.Should().BeOfType <ArgumentNullException>();
            exceptionPassedToOnFallback.Should().Be(instanceToThrow);
        }