コード例 #1
0
        public async Task HandleRetry_DirectSuccess()
        {
            var setup = new Setup();

            setup.Response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.OK);

            var retryCalled = 0;

            setup.Retry = (request) =>
            {
                retryCalled++;
                return(Task.FromResult(setup.Response.Object));
            };

            var handler = new SimpleRetryHandler(setup.Sleep.Object)
            {
                TooManyRequestsConsumesARetry = true,
                RetryTimes = 10,
                RetryAfter = TimeSpan.FromMilliseconds(50)
            };
            var response = await handler.HandleRetry(setup.Request.Object, setup.Response.Object, setup.Retry);

            Assert.AreEqual(0, retryCalled);
            Assert.AreEqual(setup.Response.Object, response);
            setup.Sleep.Verify(s => s(TimeSpan.FromMilliseconds(50)), Times.Exactly(0));
        }
コード例 #2
0
        public async Task HandleRetry_TooManyRetriesWithSuccess()
        {
            var setup = new Setup();

            setup.Response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.TooManyRequests);
            setup.Response.SetupGet(r => r.Headers).Returns(new Dictionary <string, string> {
                { "Retry-After", "50" }
            });

            var successResponse = new Mock <IResponse>();

            successResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.OK);

            var retryCalled = 0;

            setup.Retry = (request) =>
            {
                retryCalled++;
                return(Task.FromResult(successResponse.Object));
            };

            var handler = new SimpleRetryHandler(setup.Sleep.Object)
            {
                TooManyRequestsConsumesARetry = true,
                RetryTimes = 10
            };
            var response = await handler.HandleRetry(setup.Request.Object, setup.Response.Object, setup.Retry);

            Assert.AreEqual(1, retryCalled);
            Assert.AreEqual(successResponse.Object, response);
            setup.Sleep.Verify(s => s(TimeSpan.FromSeconds(50)), Times.Once);
        }
コード例 #3
0
        public void HandleRetry_WorksWithLowerCaseHeader()
        {
            var setup = new Setup();

            setup.Response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.TooManyRequests);
            setup.Response.SetupGet(r => r.Headers).Returns(new Dictionary <string, string> {
                { "retry-after", "50" }
            });

            var retryCalled = 0;

            setup.Retry = (IRequest request) =>
            {
                retryCalled++;
                return(Task.FromResult(setup.Response.Object));
            };

            var handler = new SimpleRetryHandler(setup.Sleep.Object)
            {
                TooManyRequestsConsumesARetry = true,
                RetryTimes = 1
            };

            Assert.DoesNotThrowAsync(async() =>
            {
                var response = await handler.HandleRetry(setup.Request.Object, setup.Response.Object, setup.Retry);
            });

            Assert.AreEqual(1, retryCalled);
        }