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); }
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); }
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)); }
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); } }
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(); }