Ejemplo n.º 1
0
        public async Task AddAsync_Creates_IfTrue_AndNotFound(string partitionKeyPath, int collectionThroughput)
        {
            // Arrange
            var mockService           = new Mock <IDocumentDBService>(MockBehavior.Strict);
            DocumentDBContext context = CreateContext(mockService.Object, partitionKeyPath: partitionKeyPath,
                                                      throughput: collectionThroughput, createIfNotExists: true);
            var collector = new DocumentDBAsyncCollector <Item>(context);

            mockService
            .SetupSequence(m => m.UpsertDocumentAsync(It.IsAny <Uri>(), It.IsAny <object>()))
            .Throws(DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.NotFound))
            .Returns(Task.FromResult(new Document()));

            SetupDatabaseMock(mockService);
            SetupCollectionMock(mockService, partitionKeyPath, collectionThroughput);

            // Act
            await collector.AddAsync(new Item { Text = "hello!" });

            // Assert
            mockService.VerifyAll();

            // Verify that we upsert again after creation.
            mockService.Verify(m => m.UpsertDocumentAsync(It.IsAny <Uri>(), It.IsAny <object>()), Times.Exactly(2));
        }
        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);
        }
Ejemplo n.º 3
0
        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_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 AddAsync_Retries_IfThrottled()
        {
            // Arrange
            var databases = new Database[] { new Database {
                                                 Id = DatabaseName
                                             } };
            var collections = new DocumentCollection[] { new DocumentCollection {
                                                             Id = CollectionName
                                                         } };
            var mockDocDBService = new Mock <IDocumentDBService>(MockBehavior.Strict);
            var context          = CreateContext(mockDocDBService.Object);
            var collector        = new DocumentDBAsyncCollector <Item>(context);

            mockDocDBService
            .SetupSequence(m => m.CreateDocumentAsync(It.IsAny <Uri>(), It.IsAny <object>()))
            .Throws(DocumentDBTestUtility.CreateDocumentClientException((HttpStatusCode)429))
            .Throws(DocumentDBTestUtility.CreateDocumentClientException((HttpStatusCode)429))
            .Throws(DocumentDBTestUtility.CreateDocumentClientException((HttpStatusCode)429))
            .Returns(Task.FromResult(new Document()));

            // Act
            await collector.AddAsync(new Item { Text = "hello!" });

            // Assert
            mockDocDBService.VerifyAll();
        }
        public async Task CreateIfNotExist_Throws_IfExceptionIsNotConflict()
        {
            // Arrange
            var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict);

            mockService
            .Setup(m => m.CreateDatabaseAsync(It.Is <Database>(d => d.Id == DatabaseName)))
            .ThrowsAsync(DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.BadRequest));

            // Act
            await Assert.ThrowsAsync <DocumentClientException>(
                () => DocumentDBAttributeBindingProvider.CreateIfNotExistAsync(mockService.Object, DatabaseName, CollectionName));

            // Assert
            mockService.VerifyAll();
        }
Ejemplo n.º 7
0
        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_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 CreateIfNotExist_Succeeds_IfCollectionDoesNotExist()
        {
            // Arrange
            var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict);

            mockService
            .Setup(m => m.CreateDatabaseAsync(It.Is <Database>(d => d.Id == DatabaseName)))
            .ThrowsAsync(DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.Conflict));

            mockService
            .Setup(m => m.CreateDocumentCollectionAsync(databaseUri, It.Is <DocumentCollection>(d => d.Id == CollectionName)))
            .ReturnsAsync(new DocumentCollection());

            // Act
            await DocumentDBAttributeBindingProvider.CreateIfNotExistAsync(mockService.Object, DatabaseName, CollectionName);

            // Assert
            mockService.VerifyAll();
        }
Ejemplo n.º 10
0
        public async Task AddAsync_Throws_IfExceptionIsNotTooManyRequests()
        {
            // Arrange
            var mockDocDBService = new Mock <IDocumentDBService>(MockBehavior.Strict);

            mockDocDBService
            .Setup(m => m.UpsertDocumentAsync(It.IsAny <Uri>(), It.IsAny <object>()))
            .ThrowsAsync(DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.NotFound));

            var context   = CreateContext(mockDocDBService.Object);
            var collector = new DocumentDBAsyncCollector <Item>(context);

            // Act
            await Assert.ThrowsAsync <DocumentClientException>(() => collector.AddAsync(new Item {
                Text = "hello!"
            }));

            // Assert
            mockDocDBService.VerifyAll();
        }
Ejemplo n.º 11
0
        public async Task CreateIfNotExist_Throws_IfExceptionIsNotConflict()
        {
            // Arrange
            var mockService           = new Mock <IDocumentDBService>(MockBehavior.Strict);
            DocumentDBContext context = CreateContext(mockService.Object);

            SetupDatabaseMock(mockService);

            // overwrite the default setup with one that throws
            mockService
            .Setup(m => m.CreateDatabaseAsync(It.Is <Database>(d => d.Id == DatabaseName)))
            .ThrowsAsync(DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.BadRequest));

            // Act
            await Assert.ThrowsAsync <DocumentClientException>(
                () => DocumentDBAsyncCollector <Item> .CreateIfNotExistAsync(context));

            // Assert
            mockService.VerifyAll();
        }
        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 ConvertAsync_RethrowsException_IfNotFound()
        {
            Mock <IDocumentDBService> mockService;
            var builder = CreateBuilder <Item>(out mockService);

            mockService
            .Setup(m => m.ExecuteNextAsync <Item>(_expectedUri, It.IsAny <SqlQuerySpec>(), It.IsAny <string>()))
            .ThrowsAsync(DocumentDBTestUtility.CreateDocumentClientException((HttpStatusCode)404));

            DocumentDBAttribute attribute = new DocumentDBAttribute(DatabaseName, CollectionName)
            {
                SqlQuery           = string.Empty,
                SqlQueryParameters = new SqlParameterCollection()
            };

            var ex = await Assert.ThrowsAsync <DocumentClientException>(() => builder.ConvertAsync(attribute, CancellationToken.None));

            Assert.Equal("NotFound", ex.Error.Code);

            mockService.Verify(m => m.ExecuteNextAsync <Item>(_expectedUri, It.IsAny <SqlQuerySpec>(), It.IsAny <string>()), Times.Once());
        }
Ejemplo n.º 15
0
        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);
        }
Ejemplo n.º 16
0
        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();
        }
        public async Task ConvertAsync_Retries_IfThrottled()
        {
            Mock <IDocumentDBService> mockService;
            var builder = CreateBuilder <Item>(out mockService);

            mockService
            .SetupSequence(m => m.ExecuteNextAsync <Item>(_expectedUri, It.IsAny <SqlQuerySpec>(), It.IsAny <string>()))
            .ThrowsAsync(DocumentDBTestUtility.CreateDocumentClientException((HttpStatusCode)429))
            .ThrowsAsync(DocumentDBTestUtility.CreateDocumentClientException((HttpStatusCode)429))
            .ThrowsAsync(DocumentDBTestUtility.CreateDocumentClientException((HttpStatusCode)429))
            .ReturnsAsync(new DocumentQueryResponse <Item>());

            DocumentDBAttribute attribute = new DocumentDBAttribute(DatabaseName, CollectionName)
            {
                SqlQuery           = string.Empty,
                SqlQueryParameters = new SqlParameterCollection()
            };

            var results = await builder.ConvertAsync(attribute, CancellationToken.None);

            Assert.Empty(results);

            mockService.Verify(m => m.ExecuteNextAsync <Item>(_expectedUri, It.IsAny <SqlQuerySpec>(), It.IsAny <string>()), Times.Exactly(4));
        }