public void RetryForeverFunctionTrue()
        {
            // Arrange.
            int i              = 0;
            int zero           = 0;
            int expectedReturn = 1101;
            int iterations     = 10;

            Func <int> func = () =>
            {
                i += 1;
                if (i == iterations)
                {
                    return(expectedReturn);
                }

                return(i / zero);
            };

            // Act.
            var result = Retrier.Init()
                         .WithMsWaitOf(0)
                         .RetryUntilSuccessful()
                         .Invoke(func);

            // Assert.
            Assert.Equal(expectedReturn, result.Result);
            Assert.Equal(iterations, result.RetryInfo.Executions);
            Assert.IsType <DivideByZeroException>(result.RetryInfo.Exceptions.FirstOrDefault());
            Assert.Equal(iterations - 1, result.RetryInfo.Exceptions.Count());
            Assert.False(result.Successful);
        }
        public void RetryForeverActionTrue()
        {
            // Arrange.
            int i          = 0;
            int zero       = 0;
            int iterations = 10;

            Action func = () =>
            {
                i += 1;
                if (i != iterations)
                {
                    int x = i / zero;
                }
            };

            // Act.
            var result = Retrier.Init()
                         .WithMsWaitOf(0)
                         .RetryUntilSuccessful()
                         .Invoke(func);

            // Assert.
            Assert.Equal(iterations, result.RetryInfo.Executions);
            Assert.IsType <DivideByZeroException>(result.RetryInfo.Exceptions.FirstOrDefault());
            Assert.Equal(iterations - 1, result.RetryInfo.Exceptions.Count());
            Assert.False(result.Successful);
        }
        public void RetryForeverConfigFalse()
        {
            // Arrange & Act.
            var config = Retrier.Init();

            // Assert.
            Assert.False(config.RetryForever);
        }
        public void InitRetryConfig()
        {
            // Arrange && Act.
            var config = Retrier.Init(TimeSpan.FromMilliseconds(200), 10);

            // Assert.
            Assert.Equal(200, config.WaitBetweenRetries.Milliseconds);
            Assert.Equal(10, config.MaxRetries);
        }
        public void RetryForeverConfigTrue()
        {
            // Arrange & Act.
            var config = Retrier.Init()
                         .WithMsWaitOf(0)
                         .RetryUntilSuccessful();

            // Assert.
            Assert.True(config.RetryForever);
        }
        public void RetryAsyncAction()
        {
            // Arrange && Act.
            var result = Retrier.Init()
                         .Invoke(async() => await FakeService.DoFakeCalculationsAsync());

            // Assert.
            Assert.Equal(1, result.RetryInfo.Executions);
            Assert.True(result.Successful);
        }
        public void InitDefaultRetryConfig()
        {
            // Arrange
            // Act.
            var config = Retrier.Init();

            // Assert.
            Assert.Equal(0, config.WaitBetweenRetries.Milliseconds);
            Assert.Equal(3, config.MaxRetries);
        }
        public void InitRetryConfigFluent()
        {
            // Arrange && Act.
            var config = Retrier.Init()
                         .WithNumberOfRetries(10)
                         .WithMsWaitOf(1000);

            // Assert.
            Assert.Equal(10, config.MaxRetries);
            Assert.Equal(1, config.WaitBetweenRetries.Seconds);
        }
        public void RetryAsyncFunction()
        {
            // Arrange && Act.
            var result = Retrier.Init()
                         .Invoke(async() => await FakeService.GetHelloWorldAsync());

            // Assert.
            Assert.Equal("Hello world", result.Result.Result);
            Assert.Equal(1, result.RetryInfo.Executions);
            Assert.True(result.Successful);
        }
        public void RetryActionSuccessful()
        {
            // Arrange & Act.
            var res = Retrier.Init()
                      .WithMsWaitOf(0)
                      .WithNumberOfRetries(1)
                      .Invoke(() => Trace.WriteLine("hello"));

            // Assert.
            Assert.True(res.Successful);
            Assert.False(res.ExecutedFallBack);
        }
        public async void RetryAsyncFailAction()
        {
            // Arrange & Act.
            var result = await Retrier.Init()
                         .WithNumberOfRetries(1)
                         .InvokeAsync(async() => await FakeService.DivideByZeroExceptionAsync());

            // Assert.
            Assert.False(result.Successful);
            Assert.Equal(2, result.RetryInfo.Executions);
            Assert.IsType <DivideByZeroException>(result.RetryInfo.Exceptions.FirstOrDefault());
        }
        public void RetryWithDefaultConfig()
        {
            // Arrange
            int res = 0;

            // Act.
            res = Retrier.Init()
                  .Invoke(() => 2 + 2)
                  .Result;

            // Assert.
            Assert.Equal(4, res);
        }
        public void RetryFunctionFailureWithZeroRetries()
        {
            // Arrange & Act.
            var res = Retrier.Init()
                      .WithNumberOfRetries(0)
                      .Invoke <int>(() => {
                throw new NullReferenceException();
            });

            // Assert.
            Assert.False(res.Successful);
            Assert.Equal(1, res.RetryInfo.Executions);
        }
        public void SuccessfulRetriesEqualZero()
        {
            // Arrange

            // Act.
            var res = Retrier.Init()
                      .Invoke(() => 2 + 2);

            // Assert.
            Assert.Equal(1, res.RetryInfo.Executions);
            Assert.Equal(4, res.Result);
            Assert.True(res.Successful);
            Assert.Empty(res.RetryInfo.Exceptions);
        }
Beispiel #15
0
        public void SuccessfulFunctionDoesntReturnFallbackValue()
        {
            // Arrange & Act.
            RetryResult <int> result = Retrier.Init()
                                       .WithMsWaitOf(0)
                                       .WithNumberOfRetries(2)
                                       .Invoke(() => 10 / 2)
                                       .WithFallBackValue(1000);

            // Assert.
            Assert.Equal(5, result.Result);
            Assert.True(result.Successful);
            Assert.Equal(1, result.RetryInfo.Executions);
        }
        public void CountExceptionsAndRetries()
        {
            // Arrange & Act.
            int zero = 0;
            RetryResult <int> result = Retrier.Init()
                                       .WithMsWaitOf(0)
                                       .WithNumberOfRetries(2)
                                       .Invoke(() => 2 / zero);

            // Assert.
            Assert.Equal(0, result.Result);
            Assert.False(result.Successful);
            Assert.Equal(3, result.RetryInfo.Executions);
            Assert.IsType <DivideByZeroException>(result.RetryInfo.Exceptions.FirstOrDefault());
        }
        public void RetryActionFailure()
        {
            // Arrange & Act.
            var res = Retrier.Init()
                      .WithMsWaitOf(0)
                      .WithNumberOfRetries(1)
                      .Invoke(() => {
                throw new NullReferenceException();
            });

            // Assert.
            Assert.False(res.Successful);
            Assert.False(res.ExecutedFallBack);
            Assert.IsType <NullReferenceException>(res.RetryInfo.Exceptions.FirstOrDefault());
            Assert.Equal(2, res.RetryInfo.Executions);
        }
Beispiel #18
0
        public void ExpireAfterTotalTimeoutWithRetryForever()
        {
            // Arrange & Act.
            int zero = 0;
            RetryResult <int> result = Retrier.Init()
                                       .WithWaitOf(TimeSpan.FromMilliseconds(500))
                                       .RetryUntilSuccessful()
                                       .TimeoutAfter(TimeSpan.FromSeconds(1))
                                       .Invoke(() => 2 / zero);

            // Assert.
            Assert.Equal(0, result.Result);
            Assert.False(result.Successful);
            Assert.Equal(2, result.RetryInfo.Executions);
            Assert.IsType <DivideByZeroException>(result.RetryInfo.Exceptions.FirstOrDefault());
        }
        public void InitRetryConfigDirectly()
        {
            // Arrange.
            var retryConfig = new RetryConfig
            {
                MaxRetries         = 10,
                WaitBetweenRetries = TimeSpan.FromSeconds(1)
            };

            // Act.
            var config = Retrier.Init(retryConfig);

            // Assert.
            Assert.Equal(10, config.MaxRetries);
            Assert.Equal(1, config.WaitBetweenRetries.Seconds);
        }
Beispiel #20
0
        public void RetryFunctionWithExceptionAndSuccessfulFallbackValue()
        {
            // Arrange & Act.
            int zero = 0;
            var res  = Retrier.Init()
                       .WithMsWaitOf(0)
                       .WithNumberOfRetries(1)
                       .Invoke(() => 2 / zero)
                       .WithFallBackValue(33);

            // Assert.
            Assert.Equal(33, res.Result);
            Assert.False(res.Successful);
            Assert.True(res.SuccessfulFallback);
            Assert.IsType <DivideByZeroException>(res.RetryInfo.Exceptions.FirstOrDefault());
            Assert.Null(res.FallBackException);
        }
Beispiel #21
0
        public void RetryFunctionWithExceptionAndFailFallback()
        {
            // Arrange & Act.
            int zero = 0;
            var res  = Retrier.Init()
                       .WithMsWaitOf(0)
                       .WithNumberOfRetries(1)
                       .Invoke(() => 2 / zero)
                       .WithFallBack(() => throw new ArgumentException("test"));

            // Assert.
            Assert.Equal(0, res.Result);
            Assert.False(res.Successful);
            Assert.False(res.SuccessfulFallback);
            Assert.IsType <DivideByZeroException>(res.RetryInfo.Exceptions.FirstOrDefault());
            Assert.IsType <ArgumentException>(res.FallBackException);
        }
Beispiel #22
0
        public void RetryAsyncFallbackThatFails()
        {
            // Arrange & Act.
            var result = Retrier.Init()
                         .WithNumberOfRetries(1)
                         .WithMsWaitOf(0)
                         .Invoke(FakeService.OutOfMemory)
                         .WithFallBackAsync(async() => await FakeService.DivideByZeroExceptionAsync())
                         .WaitForValue();

            // Assert.
            Assert.Equal(0, result.Result);
            Assert.Equal(3, result.RetryInfo.Executions);
            Assert.False(result.Successful);
            Assert.IsType <DivideByZeroException>(result.FallBackException);
            Assert.IsType <OutOfMemoryException>(result.RetryInfo.Exceptions.FirstOrDefault());
        }
Beispiel #23
0
        public async void RetryAsyncFailFunctionWithAsyncFallback()
        {
            // Arrange & Act.
            var result = await Retrier.Init()
                         .WithNumberOfRetries(1)
                         .InvokeAsync(FakeService.DivideByZeroExceptionAsync)
                         .WaitForValue()
                         .WithFallBackAsync(FakeService.ThrowOutOfMemoryAsync);

            // Assert.
            Assert.False(result.Successful);
            Assert.False(result.SuccessfulFallback);
            Assert.True(result.ExecutedFallBack);
            Assert.Equal(3, result.RetryInfo.Executions);
            Assert.IsType <DivideByZeroException>(result.RetryInfo.Exceptions.FirstOrDefault());
            Assert.IsType <OutOfMemoryException>(result.FallBackException);
        }
Beispiel #24
0
        public void RetryAsyncFallback()
        {
            // Arrange.
            int zero = 0;

            // Act.
            var result = Retrier.Init()
                         .WithNumberOfRetries(0)
                         .WithMsWaitOf(0)
                         .Invoke(() => 2 / zero)
                         .WithFallBackAsync(async() => await FakeService.GetIntAsync())
                         .WaitForValue();

            // Assert.
            Assert.Equal(1, result.Result);
            Assert.Equal(2, result.RetryInfo.Executions);
            Assert.False(result.Successful);
        }
Beispiel #25
0
        public void RetryAsyncFailFunctionWithSyncFallbackValue()
        {
            // Arrange.
            int expectedResult = 100;

            // Act.
            var result = Retrier.Init()
                         .WithNumberOfRetries(1)
                         .InvokeAsync(FakeService.DivideByZeroExceptionAsync)
                         .WaitForValue()
                         .WithFallBackValue(expectedResult);

            // Assert.
            Assert.Equal(expectedResult, result.Result);
            Assert.False(result.Successful);
            Assert.True(result.ExecutedFallBack);
            Assert.True(result.SuccessfulFallback);
            Assert.IsType <DivideByZeroException>(result.RetryInfo.Exceptions.FirstOrDefault());
            Assert.Equal(2, result.RetryInfo.Executions);
        }