Пример #1
0
        public void Should_create_new_state_for_each_call_to_policy()
        {
            RetryPolicy <ResultPrimitive> policy = Policy
                                                   .HandleResult(ResultPrimitive.Fault)
                                                   .Retry(1);

            policy.RaiseResultSequence(ResultPrimitive.Fault, ResultPrimitive.Good).Should().Be(ResultPrimitive.Good);

            policy.RaiseResultSequence(ResultPrimitive.Fault, ResultPrimitive.Good).Should().Be(ResultPrimitive.Good);
        }
Пример #2
0
        public void Should_not_return_handled_result_when_handled_result_raised_less_number_of_times_than_retry_count()
        {
            RetryPolicy <ResultPrimitive> policy = Policy
                                                   .HandleResult(ResultPrimitive.Fault)
                                                   .Retry(3);

            ResultPrimitive result = policy.RaiseResultSequence(ResultPrimitive.Fault, ResultPrimitive.Good);

            result.Should().Be(ResultPrimitive.Good);
        }
Пример #3
0
        public void Should_return_result_when_result_is_not_the_specified_handled_result()
        {
            RetryPolicy <ResultPrimitive> policy = Policy
                                                   .HandleResult(ResultPrimitive.Fault)
                                                   .Retry();

            ResultPrimitive result = policy.RaiseResultSequence(ResultPrimitive.FaultAgain, ResultPrimitive.Good);

            result.Should().Be(ResultPrimitive.FaultAgain);
        }
Пример #4
0
        public void Should_return_handled_result_when_handled_result_raised_more_times_then_retry_count()
        {
            RetryPolicy <ResultPrimitive> policy = Policy
                                                   .HandleResult(ResultPrimitive.Fault)
                                                   .Retry(3);

            ResultPrimitive result = policy.RaiseResultSequence(ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good);

            result.Should().Be(ResultPrimitive.Fault); // It should give up retrying after 3 retries and return the last failure, so should return Fault, not Good.
        }
Пример #5
0
        public void Should_not_return_handled_result_when_one_of_the_handled_results_raised_same_number_of_times_as_retry_count()
        {
            RetryPolicy <ResultPrimitive> policy = Policy
                                                   .HandleResult(ResultPrimitive.Fault)
                                                   .OrResult(ResultPrimitive.FaultAgain)
                                                   .Retry(3);

            ResultPrimitive result = policy.RaiseResultSequence(ResultPrimitive.Fault, ResultPrimitive.FaultAgain, ResultPrimitive.Fault, ResultPrimitive.Good);

            result.Should().Be(ResultPrimitive.Good);
        }
Пример #6
0
        public void Should_create_new_context_for_each_call_to_execute()
        {
            string contextValue = null;

            RetryPolicy <ResultPrimitive> policy = Policy
                                                   .HandleResult(ResultPrimitive.Fault)
                                                   .Retry((_, __, context) => contextValue = context["key"].ToString());

            policy.RaiseResultSequence(
                new { key = "original_value" }.AsDictionary(),
                ResultPrimitive.Fault, ResultPrimitive.Good
                );

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

            policy.RaiseResultSequence(
                new { key = "new_value" }.AsDictionary(),
                ResultPrimitive.Fault, ResultPrimitive.Good
                );

            contextValue.Should().Be("new_value");
        }
Пример #7
0
        public void Context_should_be_empty_if_execute_not_called_with_any_context_data()
        {
            Context capturedContext = null;

            RetryPolicy <ResultPrimitive> policy = Policy
                                                   .HandleResult(ResultPrimitive.Fault)
                                                   .Retry((_, __, context) => capturedContext = context);

            policy.RaiseResultSequence(ResultPrimitive.Fault, ResultPrimitive.Good);

            capturedContext.Should()
            .BeEmpty();
        }
Пример #8
0
        public void Should_not_call_onretry_when_no_retries_are_performed()
        {
            var retryCounts = new List <int>();

            RetryPolicy <ResultPrimitive> policy = Policy
                                                   .HandleResult(ResultPrimitive.Fault)
                                                   .Retry((_, retryCount) => retryCounts.Add(retryCount));

            policy.RaiseResultSequence(ResultPrimitive.Good);

            retryCounts.Should()
            .BeEmpty();
        }
Пример #9
0
        public void Should_not_call_onretry_when_retry_count_is_zero_without_context()
        {
            bool retryInvoked = false;

            Action <DelegateResult <ResultPrimitive>, int> onRetry = (_, __) => { retryInvoked = true; };

            RetryPolicy <ResultPrimitive> policy = Policy
                                                   .HandleResult(ResultPrimitive.Fault)
                                                   .Retry(0, onRetry);

            policy.RaiseResultSequence(ResultPrimitive.Fault, ResultPrimitive.Good).Should().Be(ResultPrimitive.Fault);

            retryInvoked.Should().BeFalse();
        }
Пример #10
0
        public void Should_call_onretry_on_each_retry_with_the_current_retry_count()
        {
            var expectedRetryCounts = new[] { 1, 2, 3 };
            var retryCounts         = new List <int>();

            RetryPolicy <ResultPrimitive> policy = Policy
                                                   .HandleResult(ResultPrimitive.Fault)
                                                   .Retry(3, (_, retryCount) => retryCounts.Add(retryCount));

            policy.RaiseResultSequence(ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good);

            retryCounts.Should()
            .ContainInOrder(expectedRetryCounts);
        }
Пример #11
0
        public void Should_call_onretry_with_the_passed_context()
        {
            IDictionary <string, object> contextData = null;

            RetryPolicy <ResultPrimitive> policy = Policy
                                                   .HandleResult(ResultPrimitive.Fault)
                                                   .Retry((_, __, context) => contextData = context);

            policy.RaiseResultSequence(
                new { key1 = "value1", key2 = "value2" }.AsDictionary(),
                ResultPrimitive.Fault, ResultPrimitive.Good
                )
            .Should().Be(ResultPrimitive.Good);

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