Ejemplo n.º 1
0
        public void Repeat_async_with_log_handler_should_log()
        {
            // Arrange
            Func <Task <int> > customMethodReturnWithCustomExceptionAsync = () =>
                                                                            Task.Factory.StartNew <int>(() => throw new CustomException());
            int totalAttempts = 0;
            var callback      = new FlowUtils.RetryCallback((a, e) =>
            {
                totalAttempts = a;
            });

            // Act
            try
            {
                FlowUtils.RetryAsync(
                    customMethodReturnWithCustomExceptionAsync,
                    FlowUtils.CreateCallbackRetryStrategy(callback) + FlowUtils.CreateFixedDelayRetryStrategy(2)).Wait();
            }
            catch (AggregateException)
            {
            }

            // Assert
            Assert.Equal(2, totalAttempts);
        }
Ejemplo n.º 2
0
        public void Repeat_async_with_first_fail_handler_should_work_fine()
        {
            // Arrange
            int totalAttempts = 0;

            // Act
            var result = FlowUtils.RetryAsync(
                () =>
            {
                return(Task.Factory.StartNew(() =>
                {
                    totalAttempts++;
                    if (totalAttempts < 5)
                    {
                        throw new CustomException();
                    }
                    return 10;
                }));
            },
                FlowUtils.CreateFixedDelayRetryStrategy(8)
                ).Result;

            // Assert
            Assert.Equal(10, result);
            Assert.True(totalAttempts > 4);
        }
Ejemplo n.º 3
0
        public void Repeat_async_with_fixed_retry_strategy_should_delay_correctly()
        {
            // Arrange
            Func <Task <int> > customMethodReturnWithCustomExceptionAsync = () =>
                                                                            Task.Factory.StartNew <int>(() => { throw new CustomException(); });
            var stopwatch = new Stopwatch();

            // Act
            stopwatch.Start();
            var task = FlowUtils.RetryAsync(
                customMethodReturnWithCustomExceptionAsync,
                FlowUtils.CreateFixedDelayRetryStrategy(3, TimeSpan.FromMilliseconds(50)),
                CancellationToken.None,
                typeof(CustomException));

            try
            {
                task.Wait();
            }
            catch (AggregateException)
            {
            }
            stopwatch.Stop();

            // Assert
            Assert.True(stopwatch.ElapsedMilliseconds >= 100);
        }