public void GivenDefaultRetryPolicy_WhenCallingMethodWithReturnInRetryMode_ShouldCallServiceThreeTimes()
        {
            // ARRANGE
            var service = _fixture.Fake <IRemoteService>();
            var counter = 0;

            service.MethodWithReturn().Returns(c =>
            {
                counter++;
                if (counter < 3)
                {
                    throw new Exception("Error");
                }

                return(5);
            });

            // ACT
            var actual = service.ExecuteWithPolicy(s => s.MethodWithReturn(),
                                                   PolicyFor.RetryPolicy <Exception>(new PolicyRetryOptions()));

            // ASSERT
            service.Received(3).MethodWithReturn();
            actual.Should().Be(5);
        }
        private static IReadOnlyPolicyRegistry <string> CreateRegistry()
        {
            var services = new ServiceCollection();

            services.AddPolicySupport(m =>
            {
                m.Add(ResiliencePolicy, PolicyFor.ServiceCallResilienceStrategy <Exception>(o => o.CircuitBreaker.NumberOfExceptionsBefore           = 4));
                m.Add(AsyncResiliencePolicy, PolicyFor.ServiceCallResilienceStrategyAsync <Exception>(o => o.CircuitBreaker.NumberOfExceptionsBefore = 4));
            });

            var sp = services.BuildServiceProvider();

            return(sp.GetRequiredService <IReadOnlyPolicyRegistry <string> >());
        }
        public void GivenDefaultRetryPolicy_WhenCallingVoidMethodInRetryMode_ShouldCallServiceThreeTimes()
        {
            // ARRANGE
            var service = _fixture.Fake <IRemoteService>();
            var counter = 0;

            service.When(m => m.Method()).Do(c =>
            {
                counter++;
                if (counter < 3)
                {
                    throw new Exception("Error");
                }
            });

            // ACT
            service.ExecuteWithPolicy(s => s.Method(),
                                      PolicyFor.RetryPolicy <Exception>(new PolicyRetryOptions()));

            // ASSERT
            service.Received(3).Method();
        }
        public async Task GivenDefaultRetryPolicy_WhenCallingAsyncMethodInRetryMode_ShouldCallServiceThreeTimes()
        {
            // ARRANGE
            var service = _fixture.Fake <IRemoteService>();
            var counter = 0;

            service.MethodAsync().Returns(c =>
            {
                counter++;
                if (counter < 3)
                {
                    throw new Exception("Error");
                }

                return(Task.CompletedTask);
            });

            // ACT
            await service.ExecuteWithPolicyAsync(s => s.MethodAsync(),
                                                 PolicyFor.RetryPolicyAsync <Exception>(new PolicyRetryOptions()));

            // ASSERT
            await service.Received(3).MethodAsync();
        }