Example #1
0
        public void Repeat_with_log_handler_should_log()
        {
            // Arrange
            Action customMethodReturnWithCustomException = () => throw new CustomException();
            int    totalAttempts = 0;
            var    callback      = new FlowUtils.RetryCallback((a, e) =>
            {
                totalAttempts = a;
            });

            // Act
            try
            {
                FlowUtils.Retry(
                    customMethodReturnWithCustomException,
                    FlowUtils.CreateCallbackRetryStrategy(callback) + FlowUtils.CreateFixedDelayRetryStrategy(2)
                    );
            }
            catch (CustomException)
            {
                // suppress our specific exception
            }

            // Assert
            Assert.Equal(2, totalAttempts);
        }
Example #2
0
        public void Repeat_with_fixed_retry_strategy_and_first_fast_should_work()
        {
            // Arrange
            Action customMethodReturnWithCustomException = () => throw new CustomException();
            var    stopwatch = new Stopwatch();

            // Act
            stopwatch.Start();
            try
            {
                FlowUtils.Retry(
                    customMethodReturnWithCustomException,
                    FlowUtils.CreateFixedDelayRetryStrategy(4, TimeSpan.FromMilliseconds(50), true),
                    typeof(CustomException)
                    );
            }
            catch (CustomException)
            {
                // suppress our specific exception
            }
            stopwatch.Stop();

            // Assert
            Assert.True(stopwatch.ElapsedMilliseconds >= 100);
        }
Example #3
0
        public void Repeat_with_fixed_retry_strategy_should_work()
        {
            // Arrange
            Func <int> customMethodReturn = () => 123;

            // Act & assert
            FlowUtils.Retry(customMethodReturn, FlowUtils.CreateFixedDelayRetryStrategy());
            FlowUtils.Retry(customMethodReturn, FlowUtils.CreateFixedDelayRetryStrategy(int.MaxValue, TimeSpan.MaxValue));
        }
Example #4
0
        public void Repeat_with_fixed_retry_strategy_should_throw_exceptions()
        {
            // Arrange
            Action customMethodReturnWithCustomException = () => throw new CustomException();

            // Act & assert
            Assert.Throws <CustomException>(
                () => FlowUtils.Retry(customMethodReturnWithCustomException,
                                      FlowUtils.CreateFixedDelayRetryStrategy()));
            Assert.Throws <CustomException>(
                () => FlowUtils.Retry(customMethodReturnWithCustomException,
                                      FlowUtils.CreateFixedDelayRetryStrategy(), typeof(InvalidOperationException)));
            Assert.Throws <CustomException>(
                () => FlowUtils.Retry(customMethodReturnWithCustomException,
                                      FlowUtils.CreateFixedDelayRetryStrategy(), typeof(CustomException)));
        }
Example #5
0
        public void Repeat_async_with_fixed_retry_strategy_and_first_fast_should_work()
        {
            // Arrange
            Func <Task <int> > customMethodReturnWithCustomExceptionAsync = () =>
                                                                            Task.Factory.StartNew <int>(() => throw new CustomException());
            var stopwatch = new Stopwatch();

            // Act
            stopwatch.Start();
            try
            {
                FlowUtils.Retry(
                    customMethodReturnWithCustomExceptionAsync,
                    FlowUtils.CreateFixedDelayRetryStrategy(2, TimeSpan.FromMilliseconds(50), true),
                    typeof(CustomException)).Wait();
            }
            catch (AggregateException)
            {
            }
            stopwatch.Stop();

            // Assert
            Assert.True(stopwatch.ElapsedMilliseconds <= 50);
        }