public void Repeat_async_with_first_fail_handler_should_work_fine() { // Arrange int totalAttempts = 0; // Act var result = FlowUtils.RetryAsync( () => { return(Task.Factory.StartNew(() => { totalAttempts++; if (totalAttempts < 5) { throw new CustomException(); } return 10; })); }, FlowUtils.CreateFixedDelayRetryStrategy(8) ).Result; // Assert Assert.Equal(10, result); Assert.True(totalAttempts > 4); }
public void Repeat_async_with_fixed_retry_strategy_should_delay_correctly() { // Arrange Func <Task <int> > customMethodReturnWithCustomExceptionAsync = () => Task.Factory.StartNew <int>(() => { throw new CustomException(); }); var stopwatch = new Stopwatch(); // Act stopwatch.Start(); var task = FlowUtils.RetryAsync( customMethodReturnWithCustomExceptionAsync, FlowUtils.CreateFixedDelayRetryStrategy(3, TimeSpan.FromMilliseconds(50)), CancellationToken.None, typeof(CustomException)); try { task.Wait(); } catch (AggregateException) { } stopwatch.Stop(); // Assert Assert.True(stopwatch.ElapsedMilliseconds >= 100); }
public void Repeat_async_with_log_handler_should_log() { // Arrange Func <Task <int> > customMethodReturnWithCustomExceptionAsync = () => Task.Factory.StartNew <int>(() => throw new CustomException()); int totalAttempts = 0; var callback = new FlowUtils.RetryCallback((a, e) => { totalAttempts = a; }); // Act try { FlowUtils.RetryAsync( customMethodReturnWithCustomExceptionAsync, FlowUtils.CreateCallbackRetryStrategy(callback) + FlowUtils.CreateFixedDelayRetryStrategy(2)).Wait(); } catch (AggregateException) { } // Assert Assert.Equal(2, totalAttempts); }
public void Repeat_with_log_handler_should_log() { // Arrange Action customMethodReturnWithCustomException = () => throw new CustomException(); int totalAttempts = 0; var callback = new FlowUtils.RetryCallback((a, e) => { totalAttempts = a; }); // Act try { FlowUtils.Retry( customMethodReturnWithCustomException, FlowUtils.CreateCallbackRetryStrategy(callback) + FlowUtils.CreateFixedDelayRetryStrategy(2) ); } catch (CustomException) { // suppress our specific exception } // Assert Assert.Equal(2, totalAttempts); }
public void Repeat_with_fixed_retry_strategy_and_first_fast_should_work() { // Arrange Action customMethodReturnWithCustomException = () => throw new CustomException(); var stopwatch = new Stopwatch(); // Act stopwatch.Start(); try { FlowUtils.Retry( customMethodReturnWithCustomException, FlowUtils.CreateFixedDelayRetryStrategy(4, TimeSpan.FromMilliseconds(50), true), typeof(CustomException) ); } catch (CustomException) { // suppress our specific exception } stopwatch.Stop(); // Assert Assert.True(stopwatch.ElapsedMilliseconds >= 100); }
public void Repeat_with_fixed_retry_strategy_should_work() { // Arrange Func <int> customMethodReturn = () => 123; // Act & assert FlowUtils.Retry(customMethodReturn, FlowUtils.CreateFixedDelayRetryStrategy()); FlowUtils.Retry(customMethodReturn, FlowUtils.CreateFixedDelayRetryStrategy(int.MaxValue, TimeSpan.MaxValue)); }
public void Repeat_with_fixed_retry_strategy_should_throw_exceptions() { // Arrange Action customMethodReturnWithCustomException = () => throw new CustomException(); // Act & assert Assert.Throws <CustomException>( () => FlowUtils.Retry(customMethodReturnWithCustomException, FlowUtils.CreateFixedDelayRetryStrategy())); Assert.Throws <CustomException>( () => FlowUtils.Retry(customMethodReturnWithCustomException, FlowUtils.CreateFixedDelayRetryStrategy(), typeof(InvalidOperationException))); Assert.Throws <CustomException>( () => FlowUtils.Retry(customMethodReturnWithCustomException, FlowUtils.CreateFixedDelayRetryStrategy(), typeof(CustomException))); }
public void Repeat_async_with_fixed_retry_strategy_and_first_fast_should_work() { // Arrange Func <Task <int> > customMethodReturnWithCustomExceptionAsync = () => Task.Factory.StartNew <int>(() => throw new CustomException()); var stopwatch = new Stopwatch(); // Act stopwatch.Start(); try { FlowUtils.Retry( customMethodReturnWithCustomExceptionAsync, FlowUtils.CreateFixedDelayRetryStrategy(2, TimeSpan.FromMilliseconds(50), true), typeof(CustomException)).Wait(); } catch (AggregateException) { } stopwatch.Stop(); // Assert Assert.True(stopwatch.ElapsedMilliseconds <= 50); }