public void Executing_the_policy_function_should_should_throw_when_context_data_is_null()
        {
            ContextualPolicy policy = Policy
                                      .Handle <DivideByZeroException>()
                                      .RetryAsync((_, __, ___) => { });

            policy.Invoking(p => p.ExecuteAsync(() => Task.FromResult(2), null))
            .ShouldThrow <ArgumentNullException>().And
            .ParamName.Should().Be("contextData");
        }
Пример #2
0
        public void Execute_and_capturing_the_policy_function_should_throw_when_context_data_is_null()
        {
            ContextualPolicy policy = Policy
                                      .Handle <DivideByZeroException>()
                                      .Retry((_, __, ___) => { });

            policy.Invoking(p => p.ExecuteAndCapture(() => 2, null))
            .ShouldThrow <ArgumentNullException>().And
            .ParamName.Should().Be("contextData");
        }
Пример #3
0
        public void Should_create_new_context_for_each_call_to_execute_and_capture()
        {
            string contextValue = null;

            ContextualPolicy policy = Policy
                                      .Handle <DivideByZeroException>()
                                      .Retry((_, __, context) => contextValue = context["key"].ToString());

            policy.Invoking(p => p.ExecuteAndCapture(() => { throw new DivideByZeroException(); },
                                                     new { key = "original_value" }.AsDictionary()))
            .ShouldNotThrow();

            contextValue.Should().Be("original_value");

            policy.Invoking(p => p.ExecuteAndCapture(() => { throw new DivideByZeroException(); },
                                                     new { key = "new_value" }.AsDictionary()))
            .ShouldNotThrow();

            contextValue.Should().Be("new_value");
        }
Пример #4
0
        public void Should_not_call_onretry_when_retry_count_is_zero_with_context()
        {
            bool retryInvoked = false;

            Action <Exception, TimeSpan, Context> onRetry = (_, __, ___) => { retryInvoked = true; };

            ContextualPolicy policy = Policy
                                      .Handle <DivideByZeroException>()
                                      .WaitAndRetry(0, retryAttempt => TimeSpan.FromSeconds(1), onRetry);

            policy.Invoking(x => x.RaiseException <DivideByZeroException>())
            .ShouldThrow <DivideByZeroException>();

            retryInvoked.Should().BeFalse();
        }
Пример #5
0
        public void Should_call_onretry_with_the_passed_context_when_execute_and_capture()
        {
            IDictionary <string, object> contextData = null;

            ContextualPolicy policy = Policy
                                      .Handle <DivideByZeroException>()
                                      .Retry((_, __, context) => contextData = context);

            policy.Invoking(p => p.ExecuteAndCapture(() => { throw new DivideByZeroException(); },
                                                     new { key1 = "value1", key2 = "value2" }.AsDictionary()))
            .ShouldNotThrow();

            contextData.Should()
            .ContainKeys("key1", "key2").And
            .ContainValues("value1", "value2");
        }