コード例 #1
0
        public async Task FuncAsync_Should_Be_Tried_3Times_With_ConstantInterval()
        {
            var exceptionCount = 0;
            var failureCount   = 0;
            var successCount   = 0;
            var watch          = new Stopwatch();

            watch.Start();

            await RetryLogic
            .DoAsync <int>(this.ThrowExceptionAsync)
            .Handle <FileNotFoundException>()
            .AtMost(3)
            .WithConstantInterval(TimeSpan.FromMilliseconds(2000))
            .OnException <FileNotFoundException>(e => ++ exceptionCount)
            .OnFailure(() => ++ failureCount)
            .OnSuccess(x =>
            {
                ++successCount;
            })
            .RunAsync();

            watch.Stop();

            watch.Elapsed.ShouldBeGreaterThanOrEqualTo(TimeSpan.FromMilliseconds(2000 * 2));
            exceptionCount.ShouldEqual(3);
            failureCount.ShouldEqual(1);
            successCount.ShouldEqual(0);
        }
コード例 #2
0
        public async Task ActionAsync_Should_Be_A_Success()
        {
            var exceptionCount = 0;
            var failureCount   = 0;
            var successCount   = 0;
            var watch          = new Stopwatch();

            watch.Start();

            await RetryLogic
            .DoAsync(this.ActionNoExceptionAsync)
            .Handle <FileNotFoundException>()
            .AtMost(3)
            .WithConstantInterval(TimeSpan.FromMilliseconds(2000))
            .OnException <FileNotFoundException>(e => ++ exceptionCount)
            .OnFailure(() => ++ failureCount)
            .OnSuccess(x =>
            {
                ++successCount;
            })
            .RunAsync();

            watch.Stop();

            exceptionCount.ShouldEqual(0);
            failureCount.ShouldEqual(0);
            successCount.ShouldEqual(1);
        }
コード例 #3
0
        public async Task Retry_With_Minimal_Settings_Should_Not_Fail()
        {
            var watch = new Stopwatch();

            watch.Start();
            Exception exception = null;


            await RetryLogic.DoAsync(ActionThrowExceptionAsync)
            .OnFailureContinue()
            .Handle <FileNotFoundException>()
            .RunAsync();

            watch.Stop();

            watch.Elapsed.ShouldBeGreaterThanOrEqualTo(TimeSpan.FromMilliseconds(4010));
        }
コード例 #4
0
        public async Task FuncAsync_Should_Be_Tried_3Times_With_ConstantInterval_IsTraced()
        {
            string trace    = "";
            var    listener = new TestTraceListener();

            listener.TraceWrite += (s, e) => trace += e.Message;
            Trace.Listeners.Add(listener);
            try
            {
                var exceptionCount = 0;
                var failureCount   = 0;
                var successCount   = 0;
                var watch          = new Stopwatch();
                watch.Start();

                await RetryLogic
                .DoAsync <int>(this.ThrowExceptionAsync)
                .Handle <FileNotFoundException>()
                .WithTrace(true)
                .AtMost(3)
                .WithConstantInterval(TimeSpan.FromMilliseconds(2000))
                .OnException <FileNotFoundException>(e => ++ exceptionCount)
                .OnFailure(() => ++ failureCount)
                .OnSuccess(x =>
                {
                    ++successCount;
                })
                .RunAsync();

                watch.Stop();

                watch.Elapsed.ShouldBeGreaterThanOrEqualTo(TimeSpan.FromMilliseconds(2000 * 2));
                exceptionCount.ShouldEqual(3);
                failureCount.ShouldEqual(1);
                successCount.ShouldEqual(0);
                trace.ShouldContain("Max attempt number reached");
            }
            finally
            {
                Trace.Listeners.Remove(listener);
            }
        }
コード例 #5
0
        public async Task Retry_Without_Settings_Should_Fail()
        {
            var watch = new Stopwatch();

            watch.Start();
            Exception exception = null;

            try
            {
                await RetryLogic.DoAsync(ActionThrowExceptionAsync)
                .RunAsync();
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            exception.ShouldBeType(typeof(InvalidOperationException));
            watch.Stop();
        }