コード例 #1
0
        public async Task BreakCircuitAndThrow()
        {
            var mockLogger = new Mock <ILogger>();

            mockLogger.Setup(x => x.Write(It.Is <string>(y => y.Equals("Breaking circuit")))).Verifiable();

            var pollyService = new PollyService(mockLogger.Object, 10);

            for (int i = 0; i < 10; i++)
            {
                try
                {
                    var posts = await pollyService.GetWithPolicy <string>(
                        PolicyTypes.CircuitBreaker,
                        async() =>
                    {
                        await Task.Delay(40);
                        throw new Exception("BOEM");
                    },
                        null).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    // expected
                }
            }

            mockLogger.Verify(x => x.Write(It.Is <string>(y => y.Equals("Breaking circuit"))), Times.AtLeast(1));
        }
コード例 #2
0
        public async Task BreakCircuitRetryAndFinallyReturnFallbackData()
        {
            var mockLogger = new Mock <ILogger>();

            mockLogger.Setup(x => x.Write(It.Is <string>(y => y.Equals("FallbackPolicy invoked")))).Verifiable();
            mockLogger.Setup(x => x.Write(It.Is <string>(y => y.Equals("Breaking circuit")))).Verifiable();
            mockLogger.Setup(x => x.Write(It.Is <string>(y => y.Equals("RetryPolicy invoked")))).Verifiable();

            var pollyService = new PollyService(mockLogger.Object);

            for (int i = 0; i < 10; i++)
            {
                try
                {
                    var data = await pollyService.GetWithPolicy <string>(
                        PolicyTypes.CircuitBreakerWithRetryAndFallBack,
                        () => throw new Exception("BOEM"),
                        () => Task.FromResult("test")).ConfigureAwait(false);

                    data.Should().Be("test");
                    mockLogger.Verify(x => x.Write(It.Is <string>(y => y.Equals("FallbackPolicy invoked"))), Times.AtLeastOnce());
                    mockLogger.Verify(x => x.Write(It.Is <string>(y => y.Equals("RetryPolicy invoked"))), Times.Exactly(3));
                }
                catch (Exception ex)
                {
                    // expected
                }
            }
            mockLogger.Verify(x => x.Write(It.Is <string>(y => y.Equals("Breaking circuit"))), Times.AtLeastOnce());
        }
コード例 #3
0
        public async Task ReturnFallbackData()
        {
            var mockLogger = new Mock <ILogger>();

            mockLogger.Setup(x => x.Write(It.Is <string>(y => y.Equals("FallbackPolicy invoked")))).Verifiable();
            var pollyService = new PollyService(mockLogger.Object);

            var result = await pollyService.GetWithPolicy <string>(PolicyTypes.Fallback,
                                                                   () => throw new Exception("BOEM"),
                                                                   () => Task.FromResult("test")
                                                                   );

            result.Should().Be("test");

            mockLogger.Verify(x => x.Write(It.Is <string>(y => y.Equals("FallbackPolicy invoked"))), Times.Exactly(1));
        }
コード例 #4
0
        public void RetryThreeTimesBeforeThrowing()
        {
            var mockLogger = new Mock <ILogger>();

            mockLogger.Setup(x => x.Write(It.Is <string>(y => y.Equals("RetryPolicy invoked")))).Verifiable();
            var pollyService = new PollyService(mockLogger.Object);

            var action = new Func <Task>(async() =>
                                         await pollyService.GetWithPolicy <string>(PolicyTypes.Retry,
                                                                                   () => throw new Exception("BOEM"),
                                                                                   null
                                                                                   ));

            action.Should().Throw <Exception>().WithMessage("BOEM");

            mockLogger.Verify(x => x.Write(It.Is <string>(y => y.Equals("RetryPolicy invoked"))), Times.Exactly(3));
        }
コード例 #5
0
        public async Task RetryThreeTimesAndReturnFallbackData()
        {
            var mockLogger = new Mock <ILogger>();

            mockLogger.Setup(x => x.Write(It.Is <string>(y => y.Equals("RetryPolicy invoked")))).Verifiable();

            var pollyService = new PollyService(mockLogger.Object);

            var data = await pollyService.GetWithPolicy <string>(
                PolicyTypes.RetryWithFallBack,
                () => throw new Exception("BOEM"),
                () => Task.FromResult("test")).ConfigureAwait(false);

            data.Should().Be("test");

            mockLogger.Verify(x => x.Write(It.Is <string>(y => y.Equals("RetryPolicy invoked"))), Times.Exactly(3));
        }
コード例 #6
0
        private const int FALLBACK_DELAY = 1;    // 0 gives issues with json-server

        public async Task GetAllPostsWithRetry()
        {
            try
            {
                _logger.Write(Program.SEPARATOR);
                _logger.Write($"Getting all posts with retry");
                var posts = await _pollyService.GetWithPolicy <IEnumerable <Post> >(
                    PolicyTypes.Retry,
                    () => _remoteApiService.GetAllPostsAsync(DELAY), null).ConfigureAwait(false);

                _logger.Write($"result count: {posts.Count()}");
            }
            catch (Exception)
            {
                _logger.Write("Expected exception, timeout");
            }
        }
コード例 #7
0
 public void ThrowWhenFallbackIsNull(PolicyTypes type) =>
 new Func <Task>(async() => await _pollyService.GetWithPolicy <string>(type, () => default(Task <string>), null))
 .Should().Throw <ArgumentNullException>();