Exemple #1
0
        public async void Async_SuccessOnFifthTryWith_ExecutionTimeTest()
        {
            int       i  = 0;
            Stopwatch sw = new Stopwatch();

            sw.Start();
            var result = await WaitAndRetry.RetryAsync(10, 100, () => CalledMethodSuccessOnFifthTryAsync(1));

            sw.Stop();
            Assert.InRange(sw.ElapsedMilliseconds, 400, 500);
            Assert.Equal("1", result);
            Assert.Equal(5, i);

            async Task <string> CalledMethodSuccessOnFifthTryAsync(int a)
            {
                await Task.Delay(0);

                if (++i < 5)
                {
                    throw new Exception();
                }

                return(a.ToString());
            }
        }
Exemple #2
0
        public async void AsyncVoid_SuccessOnFifthTry()
        {
            int i = 0;
            await WaitAndRetry.RetryAsync(10, 1, () => CalledVoidMethodSuccessOnFifthTryAsync(1));

            Assert.Equal(5, i);

            async Task CalledVoidMethodSuccessOnFifthTryAsync(int a)
            {
                await Task.Delay(0);

                if (++i < 5)
                {
                    throw new Exception();
                }
            }
        }
Exemple #3
0
        public async void AsyncVoid_FailedFourthTimes()
        {
            int         i   = 0;
            Func <Task> act = async() => await WaitAndRetry.RetryAsync(4, 1, () => CalledVoidMethodSuccessOnFifthTryAsync(1));

            await Assert.ThrowsAsync <RetryException>(act);

            Assert.Equal(4, i);

            async Task CalledVoidMethodSuccessOnFifthTryAsync(int a)
            {
                await Task.Delay(0);

                if (++i < 5)
                {
                    throw new Exception();
                }
            }
        }
Exemple #4
0
        public async void Async_SuccessOnFifthTry()
        {
            int i      = 0;
            var result = await WaitAndRetry.RetryAsync(10, 1, () => CalledMethodSuccessOnFifthTryAsync(1));

            Assert.Equal("1", result);
            Assert.Equal(5, i);

            async Task <string> CalledMethodSuccessOnFifthTryAsync(int a)
            {
                await Task.Delay(0);

                if (++i < 5)
                {
                    throw new Exception();
                }

                return(a.ToString());
            }
        }
Exemple #5
0
        static async Task Main(string[] args)
        {
            Service service = new Service();

            #region methods with return

            // synchronous call
            service.AttemptCount = 0;
            var result1 = WaitAndRetry.Retry <string>(10, 1, () => service.TestSuccess(1));

            // async call (both options are correct)
            service.AttemptCount = 0;
            var result21 = await WaitAndRetry.RetryAsync <string>(10, 1, () => service.TestSuccessAsync(1));

            service.AttemptCount = 0;
            var result23 = await WaitAndRetry.RetryAsync <string>(10, 1, async() => await service.TestSuccessAsync(1));

            #endregion

            #region Void Methods

            // synchronous call
            service.AttemptCount = 0;
            WaitAndRetry.Retry(10, 1, () => service.TestVoidSuccess(1));

            // async call (both options are correct)
            service.AttemptCount = 0;
            await WaitAndRetry.RetryAsync(10, 1, () => service.TestVoidSuccessAsync(1));

            service.AttemptCount = 0;
            await WaitAndRetry.RetryAsync(10, 1, async() => await service.TestVoidSuccessAsync(1));

            // This is wrong usage  !!!
            service.AttemptCount = 0;
            await WaitAndRetry.Retry(10, 1, () => service.TestVoidSuccessAsync(1));

            #endregion
        }
Exemple #6
0
        public async void Async_FailOnFirstTry()
        {
            async Task Act() => await WaitAndRetry.RetryAsync(1, 1, () => CalledMethodFailAsync(1));

            await Assert.ThrowsAsync <RetryException>(Act);
        }
Exemple #7
0
        public async void Async_SuccessOnFirstTry()
        {
            var result = await WaitAndRetry.RetryAsync(1, 1, () => CalledMethodSuccessAsync(1));

            Assert.Equal("1", result);
        }
Exemple #8
0
 public async void AsyncVoid_SuccessOnFirstTry()
 {
     await WaitAndRetry.RetryAsync(1, 1, () => CalledVoidMethodSuccessAsync(1));
 }