public void ShouldRetryHttpStatusCodes(HttpStatusCode statusCode, int maxFailCount, int expectedTryCount) { var tryCount = 0; var shouldRetry = new HttpTransientShouldRetry(maxFailCount); var nextWait = new NoWait(); var strategy = new RetryStrategy(shouldRetry, nextWait); try { Retry.Run(() => { tryCount++; // make sure it retries the number of expected times by throwing an exception var response = HttpMock.CreateWebResponse(statusCode, null); // using WebExceptionStatus.MessageLengthLimitExceeded because that is one that never triggers a retry // so we can be sure that we are testing the HttpStatusCode instead of WebException.Status throw new WebException("test", null, WebExceptionStatus.MessageLengthLimitExceeded, response); }, strategy); Assert.Fail("should have thrown an exception"); } catch (Exception) { Assert.AreEqual(expectedTryCount, tryCount); } }
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 ShouldRetryTimeoutException() { var tryCount = 0; var shouldRetry = new HttpTransientShouldRetry(5); var nextWait = new NoWait(); var strategy = new RetryStrategy(shouldRetry, nextWait); try { Retry.Run(() => { tryCount++; throw new TimeoutException("test"); }, strategy); Assert.Fail("should have thrown an exception"); } catch (Exception) { Assert.AreEqual(5, 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); } }
public void ShouldNotRetryNonWebErrors(int maxRetries) { var tryCount = 0; var shouldRetry = new HttpTransientShouldRetry(maxRetries); var nextWait = new NoWait(); var strategy = new RetryStrategy(shouldRetry, nextWait); try { Retry.Run(() => { tryCount++; // 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(tryCount == 1); } }
public void ShouldRetrySomeWebExceptions(WebExceptionStatus statusCode, int maxFailCount, int expectedTryCount) { Assert.IsTrue(expectedTryCount <= maxFailCount, "Invalid expectedTryCount or maxFailCount"); var tryCount = 0; var shouldRetry = new HttpTransientShouldRetry(maxFailCount); var nextWait = new NoWait(); var strategy = new RetryStrategy(shouldRetry, nextWait); try { Retry.Run(() => { tryCount++; throw new WebException("test", statusCode); }, strategy); Assert.Fail("should have thrown an exception"); } catch (Exception) { Assert.AreEqual(expectedTryCount, tryCount); } }