public async Task ShouldRetryExpectedNumberOfTimesAsync(int maxFailCount, int expectedTryCount) { var tryCount = 0; var shouldRetry = new MaxCountShouldRetry(maxFailCount); var nextWait = new NoWait(); var strategy = new RetryStrategy(shouldRetry, nextWait); try { await Retry.RunAsync(async() => { tryCount++; await Task.Delay(10); throw new NotSupportedException(); }, strategy); Assert.Fail("should have thrown an exception"); } catch (Exception ex) { // making sure this is raised as NotSupportedException instead of AggregateException Assert.IsInstanceOf(typeof(NotSupportedException), ex); Assert.AreEqual(expectedTryCount, tryCount); } }
public void ShouldRetryExpectedNumberOfTimes(int maxFailCount, int expectedTryCount) { var tryCount = 0; var shouldRetry = new MaxCountShouldRetry(maxFailCount); var nextWait = new NoWait(); var strategy = new RetryStrategy(shouldRetry, nextWait); try { Retry.Run(() => { tryCount++; throw new Exception("test"); }, strategy); Assert.Fail("should have thrown an exception"); } catch (Exception) { Assert.AreEqual(expectedTryCount, tryCount); } }
public void ShouldRetryMaxTimes(int maxFailCount, int expectedTryCount) { var runCount = 0; var shouldRetry = new MaxCountShouldRetry(maxFailCount); var nextWait = new NoWait(); var strategy = new RetryStrategy(shouldRetry, nextWait); try { Retry.Run(() => { runCount++; // make sure it retries the number of expected times by throwing an exception throw new Exception(); }, strategy); Assert.Fail("should have thrown an exception"); } catch (Exception) { Assert.IsTrue(runCount == expectedTryCount); } }