예제 #1
0
        public void AddResponseToRetryOn_RetriesOnConfiguredResponse_ForResponseType()
        {
            var service = Substitute.For <ITestService>();

            var failResponse = new Response {
                ResponseMessage = "fail"
            };
            var successResponse = new Response {
                ResponseMessage = "success"
            };

            service
            .TestMethodComplex(Arg.Any <Request>())
            .Returns(failResponse, failResponse, successResponse);

            var actionInvoker = new RetryingWcfActionInvoker <ITestService>(
                () => service,
                () => new ConstantDelayPolicy(TimeSpan.FromMilliseconds(50)),
                retryCount: 2);

            actionInvoker.AddResponseToRetryOn <Response>(r => r.ResponseMessage == failResponse.ResponseMessage);

            var response = actionInvoker.Invoke(s => s.TestMethodComplex(new Request()));

            Assert.That(response.ResponseMessage, Is.EqualTo(successResponse.ResponseMessage));
        }
예제 #2
0
        public void AddResponseToRetryOn_RetriesOnMatchedPredicate_WhenMultiplePredicatesAreRegistered()
        {
            var service = Substitute.For <ITestService>();

            var firstFailResponse = new Response {
                StatusCode = 100
            };
            var secondFailResponse = new Response {
                StatusCode = 101
            };
            var successResponse = new Response {
                StatusCode = 1
            };

            service
            .TestMethodComplex(Arg.Any <Request>())
            .Returns(firstFailResponse, secondFailResponse, successResponse);

            var actionInvoker = new RetryingWcfActionInvoker <ITestService>(
                () => service,
                () => new ConstantDelayPolicy(TimeSpan.FromMilliseconds(50)),
                retryCount: 2);

            actionInvoker.AddResponseToRetryOn <IResponseStatus>(r => r.StatusCode == firstFailResponse.StatusCode);
            actionInvoker.AddResponseToRetryOn <IResponseStatus>(r => r.StatusCode == secondFailResponse.StatusCode);

            var response = actionInvoker.Invoke(s => s.TestMethodComplex(new Request()));

            Assert.That(response.StatusCode, Is.EqualTo(successResponse.StatusCode));
        }
        public void Benchmark_RetryingWcfActionInvoker_Invoke()
        {
            var service = Substitute.For <ITestService>();

            service
            .TestMethodComplex(Arg.Any <Request>())
            .Returns(new Response {
                StatusCode = 1
            });

            var actionInvoker = new RetryingWcfActionInvoker <ITestService>(
                () => service);

            actionInvoker.AddResponseToRetryOn <IResponseStatus>(r => r.StatusCode == 100);

            var sw = Stopwatch.StartNew();

            foreach (var i in Enumerable.Range(0, 300000))
            {
                var response = actionInvoker.Invoke(s => s.TestMethodComplex(new Request()));
                Assert.That(response.StatusCode, Is.EqualTo(1));
            }
            sw.Stop();

            Trace.WriteLine(string.Format("Complete in {0}", sw.Elapsed));
        }
예제 #4
0
        public void AddResponseToRetryOn_RetriesOnConfiguredResponse_ForResponseType()
        {
            var mockService = new Mock <ITestService>();

            var failResponse = new Response {
                ResponseMessage = "fail"
            };
            var successResponse = new Response {
                ResponseMessage = "success"
            };

            mockService
            .SetupSequence(m => m.TestMethodComplex(It.IsAny <Request>()))
            .Returns(failResponse)
            .Returns(failResponse)
            .Returns(successResponse);

            var actionInvoker = new RetryingWcfActionInvoker <ITestService>(
                () => new TestServiceImpl(mockService),
                () => new ConstantDelayPolicy(TimeSpan.FromMilliseconds(50)),
                retryCount: 2);

            actionInvoker.AddResponseToRetryOn <Response>(r => r.ResponseMessage == failResponse.ResponseMessage);

            var response = actionInvoker.Invoke(s => s.TestMethodComplex(new Request()));

            Assert.That(response.ResponseMessage, Is.EqualTo(successResponse.ResponseMessage));
        }
예제 #5
0
        public void AddExceptionToRetryOn_PassesThroughException_OnConfiguredException_WhenPredicateDoesNotMatch()
        {
            var mockService = new Mock <ITestService>();

            mockService.Setup(m => m.TestMethod(It.IsAny <string>())).Throws <TimeoutException>();

            var actionInvoker = new RetryingWcfActionInvoker <ITestService>(
                () => new TestServiceImpl(mockService),
                () => new ConstantDelayPolicy(TimeSpan.FromMilliseconds(50)));

            actionInvoker.AddExceptionToRetryOn <TimeoutException>(where : e => e.Message == "not the message");

            Assert.That(() => actionInvoker.Invoke(s => s.TestMethod("test")), Throws.TypeOf <TimeoutException>());
        }
        public void Benchmark_RetryingWcfActionInvoker_Invoke()
        {
            var service = Substitute.For<ITestService>();

            service
                .TestMethodComplex(Arg.Any<Request>())
                .Returns(new Response { StatusCode = 1 });

            var actionInvoker = new RetryingWcfActionInvoker<ITestService>(
                () => service);

            actionInvoker.AddResponseToRetryOn<IResponseStatus>(r => r.StatusCode == 100);

            var sw = Stopwatch.StartNew();
            foreach (var i in Enumerable.Range(0, 300000))
            {
                var response = actionInvoker.Invoke(s => s.TestMethodComplex(new Request()));
                Assert.That(response.StatusCode, Is.EqualTo(1));
            }
            sw.Stop();

            Trace.WriteLine(string.Format("Complete in {0}", sw.Elapsed));
        }
예제 #7
0
        private void AssertThatCallRetriesOnException <TException>(
            Action <RetryingWcfActionInvoker <ITestService> > configurator = null,
            Func <TException> exceptionFactory = null)
            where TException : Exception, new()
        {
            var service = Substitute.For <ITestService>();

            if (exceptionFactory != null)
            {
                service
                .TestMethod(Arg.Any <string>())
                .Throws(exceptionFactory());
            }
            else
            {
                service
                .TestMethod(Arg.Any <string>())
                .Throws <TException>();
            }

            var delayPolicy = Substitute.For <IDelayPolicy>();

            var actionInvoker = new RetryingWcfActionInvoker <ITestService>(
                () => service,
                () => delayPolicy,
                retryCount: 5);

            configurator?.Invoke(actionInvoker);

            Assert.That(
                () => actionInvoker.Invoke(s => s.TestMethod("test")),
                Throws.TypeOf <WcfRetryFailedException>());

            delayPolicy
            .Received(5)
            .GetDelay(Arg.Is <int>(i => i >= 0 && i <= 4));
        }
예제 #8
0
        private void AssertThatCallRetriesOnException <TException>(
            Action <RetryingWcfActionInvoker <ITestService> > configurator = null,
            Func <TException> exceptionFactory = null)
            where TException : Exception, new()
        {
            var mockService = new Mock <ITestService>();

            if (exceptionFactory != null)
            {
                mockService.Setup(m => m.TestMethod(It.IsAny <string>())).Throws(exceptionFactory());
            }
            else
            {
                mockService.Setup(m => m.TestMethod(It.IsAny <string>())).Throws <TException>();
            }

            var mockDelayPolicy = new Mock <IDelayPolicy>();

            var actionInvoker = new RetryingWcfActionInvoker <ITestService>(
                () => new TestServiceImpl(mockService),
                () => mockDelayPolicy.Object,
                retryCount: 5);

            if (configurator != null)
            {
                configurator(actionInvoker);
            }

            Assert.That(
                () => actionInvoker.Invoke(s => s.TestMethod("test")),
                Throws.TypeOf <WcfRetryFailedException>());

            mockDelayPolicy.Verify(
                m => m.GetDelay(It.IsInRange(0, 4, Range.Inclusive)),
                Times.Exactly(5));
        }