public async Task RetryAsync_Retries_IfStatusCode429() { // Arrange var mockUri = new Uri("https://someuri"); var mockItem = new Item(); var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict); // make sure that we sleep here var tooManyRequestException = DocumentDBTestUtility.CreateDocumentClientException((HttpStatusCode)429, 1000); var document = new Document(); mockService .SetupSequence(m => m.UpsertDocumentAsync(mockUri, mockItem)) .Throws(tooManyRequestException) .Throws(tooManyRequestException) .Returns(Task.FromResult(document)); var start = DateTime.UtcNow; // Act var result = await DocumentDBUtility.RetryAsync(() => { return(mockService.Object.UpsertDocumentAsync(mockUri, mockItem)); }, 3, HttpStatusCode.NotFound); // Assert var stop = DateTime.UtcNow; Assert.True((stop - start).TotalMilliseconds >= 2000); Assert.Same(document, result); mockService.Verify(m => m.UpsertDocumentAsync(mockUri, mockItem), Times.Exactly(3)); }
public async Task RetryAsync_Throws_IfErrorStatusCode() { // Arrange var mockUri = new Uri("https://someuri"); var mockItem = new Item(); var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict); var notFoundException = DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.NotFound); mockService .Setup(m => m.UpsertDocumentAsync(mockUri, mockItem)) .Throws(notFoundException); // Act var ex = await Assert.ThrowsAsync <DocumentClientException>(() => { return(DocumentDBUtility.RetryAsync(() => { return mockService.Object.UpsertDocumentAsync(mockUri, mockItem); }, 3)); }); // Assert mockService.VerifyAll(); Assert.Equal(HttpStatusCode.NotFound, ex.StatusCode); }
public async Task ExecuteWithRetriesAsync_Throws_IfErrorStatusCode() { // Arrange var mockUri = new Uri("https://someuri"); var mockItem = new Item(); var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict); var serviceUnavailableException = DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.ServiceUnavailable); mockService .Setup(m => m.CreateDocumentAsync(mockUri, mockItem)) .Throws(serviceUnavailableException); // Act var ex = await Assert.ThrowsAsync <DocumentClientException>(() => { return(DocumentDBUtility.ExecuteWithRetriesAsync(() => { return mockService.Object.CreateDocumentAsync(mockUri, mockItem); })); }); // Assert mockService.VerifyAll(); Assert.Equal(HttpStatusCode.ServiceUnavailable, ex.StatusCode); }
public async Task RetryAsync_Ignores_SpecifiedStatusCode() { // Arrange string testString = "Method called!"; string s = null; var ex = DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.Conflict); // Act await DocumentDBUtility.RetryAsync <object>(() => { s = testString; throw ex; }, 0, HttpStatusCode.Conflict, HttpStatusCode.NotFound); // Assert Assert.Equal(testString, s); // The fact that it doesn't throw proves that it was ignored }
public async Task ExecuteAndIgnoreStatusCode_Ignores_SpecifiedStatusCode() { // Arrange string testString = "Method called!"; string s = null; var ex = DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.Conflict); // Act await DocumentDBUtility.ExecuteAndIgnoreStatusCodeAsync(HttpStatusCode.Conflict, () => { s = testString; throw ex; }); // Assert Assert.Equal(testString, s); // The fact that it doesn't throw proves that it was ignored }
public async Task RetryAsync_MaxRetries(int maxRetries) { // Arrange var mockUri = new Uri("https://someuri"); var mockItem = new Item(); var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict); var tooManyRequestException = DocumentDBTestUtility.CreateDocumentClientException((HttpStatusCode)429); mockService.Setup(m => m.UpsertDocumentAsync(mockUri, mockItem)).Throws(tooManyRequestException); // Act var docEx = await Assert.ThrowsAsync <DocumentClientException>(() => DocumentDBUtility.RetryAsync(() => { return(mockService.Object.UpsertDocumentAsync(mockUri, mockItem)); }, maxRetries)); // Assert Assert.Same(tooManyRequestException, docEx); mockService.Verify(m => m.UpsertDocumentAsync(mockUri, mockItem), Times.Exactly(maxRetries + 1)); }
public async Task RetryAsync_DoesNotIgnore_OtherStatusCodes(HttpStatusCode[] codesToIgnore) { // Arrange string testString = "Method called!"; string s = null; var ex = DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.ServiceUnavailable); // Act var thrownEx = await Assert.ThrowsAsync <DocumentClientException>(() => { return(DocumentDBUtility.RetryAsync <object>(() => { s = testString; throw ex; }, 0, codesToIgnore)); }); // Assert Assert.Equal(testString, s); Assert.Equal(HttpStatusCode.ServiceUnavailable, thrownEx.StatusCode); }
public async Task ExecuteAndIgnoreStatusCode_DoesNotIgnore_OtherStatusCodes() { // Arrange string testString = "Method called!"; string s = null; var ex = DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.ServiceUnavailable); // Act var thrownEx = await Assert.ThrowsAsync <DocumentClientException>(() => { return(DocumentDBUtility.ExecuteAndIgnoreStatusCodeAsync(HttpStatusCode.Conflict, () => { s = testString; throw ex; })); }); // Assert Assert.Equal(testString, s); Assert.Equal(HttpStatusCode.ServiceUnavailable, thrownEx.StatusCode); }
public async Task ExecuteWithRetriesAsync_Retries_IfStatusCode429() { // Arrange var mockUri = new Uri("https://someuri"); var mockItem = new Item(); var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict); var tooManyRequestException = DocumentDBTestUtility.CreateDocumentClientException((HttpStatusCode)429); mockService .SetupSequence(m => m.CreateDocumentAsync(mockUri, mockItem)) .Throws(tooManyRequestException) .Throws(tooManyRequestException) .Returns(Task.FromResult(new Document())); // Act await DocumentDBUtility.ExecuteWithRetriesAsync(() => { return(mockService.Object.CreateDocumentAsync(mockUri, mockItem)); }); // Assert mockService.VerifyAll(); }