Пример #1
0
        public void SyncVoid_SuccessOnFifthTry()
        {
            int i = 0;

            WaitAndRetry.Retry(10, 1, () => CalledVoidMethodSuccessOnFifthTry(1, ref i));
            Assert.Equal(5, i);
        }
Пример #2
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());
            }
        }
Пример #3
0
        public void SyncVoid_FailedFourthTimes()
        {
            int    i   = 0;
            Action act = () => WaitAndRetry.Retry(4, 1, () => CalledVoidMethodSuccessOnFifthTry(1, ref i));

            Assert.Throws <RetryException>(act);
            Assert.Equal(4, i);
        }
Пример #4
0
        public void Sync_SuccessOnFifthTry()
        {
            int i      = 0;
            var result = WaitAndRetry.Retry(10, 1, () => CalledMethodSuccessOnFifthTry(1, ref i));

            Assert.Equal("1", result);
            Assert.Equal(5, i);
        }
Пример #5
0
        public void SyncVoid_SuccessOnFifthTryWith_ExecutionTimeTest()
        {
            int       i  = 0;
            Stopwatch sw = new Stopwatch();

            sw.Start();
            WaitAndRetry.Retry(10, 100, () => CalledVoidMethodSuccessOnFifthTry(1, ref i));
            sw.Stop();
            Assert.InRange(sw.ElapsedMilliseconds, 400, 500);
            Assert.Equal(5, i);
        }
Пример #6
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();
                }
            }
        }
Пример #7
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();
                }
            }
        }
Пример #8
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());
            }
        }
Пример #9
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
        }
Пример #10
0
        public void SyncVoid_FailOnFirstTry()
        {
            void Act() => WaitAndRetry.Retry(1, 1, () => CalledVoidMethodFail(1));

            Assert.Throws <RetryException>(Act);
        }
Пример #11
0
 public void SyncVoid_SuccessOnFirstTry()
 {
     WaitAndRetry.Retry(1, 1, () => CalledVoidMethodSuccess(1));
 }
Пример #12
0
        public void Sync_SuccessOnFirstTry()
        {
            var result = WaitAndRetry.Retry(1, 1, () => CalledMethodSuccess(1));

            Assert.Equal("1", result);
        }
Пример #13
0
        public async void Async_FailOnFirstTry()
        {
            async Task Act() => await WaitAndRetry.RetryAsync(1, 1, () => CalledMethodFailAsync(1));

            await Assert.ThrowsAsync <RetryException>(Act);
        }
Пример #14
0
        public async void Async_SuccessOnFirstTry()
        {
            var result = await WaitAndRetry.RetryAsync(1, 1, () => CalledMethodSuccessAsync(1));

            Assert.Equal("1", result);
        }
Пример #15
0
 public async void AsyncVoid_SuccessOnFirstTry()
 {
     await WaitAndRetry.RetryAsync(1, 1, () => CalledVoidMethodSuccessAsync(1));
 }