Exemplo n.º 1
0
        public async Task AddHttpClient_WhenPollyRetryPolicyIsSet_NoErrorOccurs_NoRetriesAreAttempted()
        {
            // Arrange
            var expectedStausCode = HttpStatusCode.OK;

            SetupMockDelegate(expectedStausCode);
            IDummyService dummyService = CreateDummyService(_mockDelegatingHandler);

            // Act
            var response = await dummyService.MakeHttpCallAsync();

            // Assert
            response.StatusCode.Should().Be(expectedStausCode);
            VerifyHttpRequests(Times.Once());
        }
Exemplo n.º 2
0
        public async Task AddHttpClient_WhenPollyRetryPolicyIsSet_SingleServiceErrorsOccur_OperationIsRetried()
        {
            // Arrange
            int retryCount        = 0;
            var expectedStausCode = HttpStatusCode.OK;

            var mockDelegate = Mock.Of <DelegatingHandler>(MockBehavior.Strict);

            Mock.Get(mockDelegate)
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .Returns(() =>
            {
                // First attempt will result in failure
                retryCount++;
                if (retryCount > 1)
                {
                    return(Task.FromResult(new HttpResponseMessage(expectedStausCode)));
                }
                else
                {
                    return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.InternalServerError)));
                }
            });

            IDummyService dummyService = CreateDummyService(mockDelegate);

            // Act
            Func <Task <HttpResponseMessage> > act = async() => await dummyService.MakeHttpCallAsync();

            // By default, first call to Act is not sufficent to trigger circuit breaker
            var response = await act();

            // Assert
            response.StatusCode.Should().Be(expectedStausCode);

            Mock.Get(mockDelegate)
            .Protected()
            .Verify(
                "SendAsync",
                Times.Exactly(2),
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>());
        }
Exemplo n.º 3
0
        public async Task AddHttpClient_When_PollyRetryPolicyIsSet_And_MultipleConsecutiveSericeErrorsOccur_Then_CircuitBreakerIsTriggered()
        {
            // Arrange
            var expectedStausCode = HttpStatusCode.InternalServerError;

            SetupMockDelegate(expectedStausCode);
            IDummyService dummyService = CreateDummyService(_mockDelegatingHandler);

            // Act
            Func <Task <HttpResponseMessage> > act = async() => await dummyService.MakeHttpCallAsync();

            // By default, first call to Act is not sufficent to trigger circuit breaker
            var response = await act();

            // Assert
            response.StatusCode.Should().Be(expectedStausCode);

            // Second call has caused sufficent number of failures to trigger
            act.Should().Throw <BrokenCircuitException <HttpResponseMessage> >();
            VerifyHttpRequests(Times.Exactly(5));
        }