Esempio n. 1
0
            public async Task Specified_Action_Is_Executed_Once_Breaker_Duration_Has_Expired_On_Open_Breaker()
            {
                // Arrange
                const int exceptionsAllowedBeforeBreaking = 5;
                var       breakDuration = TimeSpan.FromSeconds(1);
                var       configuration = new PollyCircuitBreakerConfiguration
                {
                    ExceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking,
                    DurationOfBreak = breakDuration
                };
                var testCircuitBreaker = new PollyCircuitBreaker(configuration);

                OpenCircuitBreaker(_circuitBreaker);

                var testActionExecuted = false;

                Task TestAction(CancellationToken token)
                {
                    testActionExecuted = true;
                    return(Task.CompletedTask);
                }

                // Act
                Thread.Sleep(breakDuration);
                await testCircuitBreaker.ExecuteAsync(TestAction, exception => Task.CompletedTask, _cts.Token);

                // Assert
                Assert.That(testActionExecuted, Is.True);
            }
Esempio n. 2
0
            public async Task OnReset_Event_Is_Raised_When_ExecuteAction_Completes_Successfully_After_Break_Duration_Has_Expired()
            {
                // Arrange
                const int exceptionsAllowedThreshold = 1;
                TimeSpan  breakDuration = TimeSpan.FromSeconds(1);
                var       configuration = new PollyCircuitBreakerConfiguration
                {
                    ExceptionsAllowedBeforeBreaking = exceptionsAllowedThreshold,
                    DurationOfBreak = breakDuration
                };
                var circuitResetEventTriggered = false;
                var circuitBreaker             = new PollyCircuitBreaker(configuration);

                circuitBreaker.CircuitBreakerReset += (sender, args) => circuitResetEventTriggered = true;

                // Act
                await ThrowExceptionsInCircuitBreaker(circuitBreaker, numberOfExceptions : exceptionsAllowedThreshold);

                Thread.Sleep(breakDuration);
                await circuitBreaker.ExecuteAsync(
                    token => Task.CompletedTask,
                    exception => Task.CompletedTask,
                    _cts.Token);

                // Assert
                Assert.That(circuitResetEventTriggered, Is.True, "the Circuit Breaker Reset Event should be triggered");
            }
Esempio n. 3
0
            public async Task Circuit_Breaker_Runs_FailureAction_When_Exception_Occurs_In_Specified_Action()
            {
                // Arrange
                var allowedExceptionCount = 2;
                var breakerDuration       = TimeSpan.FromSeconds(2);

                Task TestAction(CancellationToken token)
                {
                    throw new ApplicationException();
                }

                Exception receivedException = null;

                Task TestFailureAction(Exception ex)
                {
                    receivedException = ex;
                    return(Task.CompletedTask);
                }

                var configuration = new PollyCircuitBreakerConfiguration
                {
                    ExceptionsAllowedBeforeBreaking = allowedExceptionCount,
                    DurationOfBreak = breakerDuration
                };

                var testCircuitBreaker = new PollyCircuitBreaker(configuration);

                // Act
                await testCircuitBreaker.ExecuteAsync(TestAction, TestFailureAction, _cts.Token);

                // Assert
                Assert.That(receivedException, Is.Not.Null);
                Assert.That(receivedException, Is.TypeOf <ApplicationException>());
            }