Exemple #1
0
        public async Task RetriesOnPreconditionFailed()
        {
            string itemId = "1";

            Cosmos.PartitionKey      partitionKey  = new Cosmos.PartitionKey("1");
            DocumentServiceLeaseCore leaseToUpdate = new DocumentServiceLeaseCore();

            Mock <ContainerCore> mockedItems = new Mock <ContainerCore>();

            mockedItems.Setup(i => i.ReadItemStreamAsync(
                                  It.Is <string>((id) => id == itemId),
                                  It.Is <Cosmos.PartitionKey>(pk => pk.Equals(partitionKey)),
                                  It.IsAny <ItemRequestOptions>(),
                                  It.IsAny <CancellationToken>()))
            .ReturnsAsync(() =>
            {
                return(new ResponseMessage(HttpStatusCode.OK)
                {
                    Content = new CosmosJsonDotNetSerializer().ToStream(leaseToUpdate)
                });
            });

            mockedItems.SetupSequence(i => i.ReplaceItemStreamAsync(
                                          It.IsAny <Stream>(),
                                          It.Is <string>((id) => id == itemId),
                                          It.Is <Cosmos.PartitionKey>(pk => pk.Equals(partitionKey)),
                                          It.IsAny <ItemRequestOptions>(),
                                          It.IsAny <CancellationToken>()))
            .Returns(() =>
            {
                return(Task.FromResult(new ResponseMessage(HttpStatusCode.PreconditionFailed)));
            })
            .Returns(() =>
            {
                return(Task.FromResult(new ResponseMessage(HttpStatusCode.OK)
                {
                    Content = new CosmosJsonDotNetSerializer().ToStream(leaseToUpdate)
                }));
            });

            var updater      = new DocumentServiceLeaseUpdaterCosmos(DocumentServiceLeaseUpdaterCosmosTests.GetMockedContainer(mockedItems));
            var updatedLease = await updater.UpdateLeaseAsync(leaseToUpdate, itemId, partitionKey, serverLease =>
            {
                serverLease.Owner = "newHost";
                return(serverLease);
            });

            Mock.Get(mockedItems.Object)
            .Verify(items => items.ReplaceItemStreamAsync(
                        It.IsAny <Stream>(),
                        It.Is <string>((id) => id == itemId),
                        It.Is <Cosmos.PartitionKey>(pk => pk.Equals(partitionKey)),
                        It.IsAny <ItemRequestOptions>(),
                        It.IsAny <CancellationToken>()), Times.Exactly(2));
            Mock.Get(mockedItems.Object)
            .Verify(items => items.ReadItemStreamAsync(It.Is <string>((id) => id == itemId),
                                                       It.Is <Cosmos.PartitionKey>(pk => pk.Equals(partitionKey)),
                                                       It.IsAny <ItemRequestOptions>(),
                                                       It.IsAny <CancellationToken>()), Times.Once);
        }
Exemple #2
0
        public async Task ThrowsAfterMaxRetries()
        {
            string itemId       = "1";
            object partitionKey = "1";
            DocumentServiceLeaseCore leaseToUpdate = new DocumentServiceLeaseCore();

            Mock <CosmosItems> mockedItems = new Mock <CosmosItems>();

            mockedItems.Setup(i => i.ReadItemAsync <DocumentServiceLeaseCore>(
                                  It.Is <object>((pk) => pk == partitionKey),
                                  It.Is <string>((id) => id == itemId),
                                  It.IsAny <CosmosItemRequestOptions>(),
                                  It.IsAny <CancellationToken>()))
            .ReturnsAsync(() =>
            {
                var itemResponse = new Mock <CosmosItemResponse <DocumentServiceLeaseCore> >();
                itemResponse.Setup(i => i.Resource).Returns(leaseToUpdate);
                return(itemResponse.Object);
            });

            mockedItems.Setup(i => i.ReplaceItemAsync <DocumentServiceLeaseCore>(
                                  It.Is <object>((pk) => pk == partitionKey),
                                  It.Is <string>((id) => id == itemId),
                                  It.Is <DocumentServiceLeaseCore>((lease) => lease == leaseToUpdate),
                                  It.IsAny <CosmosItemRequestOptions>(),
                                  It.IsAny <CancellationToken>()))
            .Throws(new CosmosException(string.Empty, HttpStatusCode.PreconditionFailed, 0, string.Empty, 0));

            var updater      = new DocumentServiceLeaseUpdaterCosmos(DocumentServiceLeaseUpdaterCosmosTests.GetMockedContainer(mockedItems.Object));
            var updatedLease = await updater.UpdateLeaseAsync(leaseToUpdate, itemId, partitionKey, serverLease =>
            {
                serverLease.Owner = "newHost";
                return(serverLease);
            });
        }
Exemple #3
0
        public async Task RetriesOnPreconditionFailed()
        {
            string itemId = "1";

            Cosmos.PartitionKey      partitionKey  = new Cosmos.PartitionKey("1");
            DocumentServiceLeaseCore leaseToUpdate = new DocumentServiceLeaseCore();

            Mock <CosmosContainerCore> mockedItems = new Mock <CosmosContainerCore>();

            mockedItems.Setup(i => i.ReadItemAsync <DocumentServiceLeaseCore>(
                                  It.Is <Cosmos.PartitionKey>(pk => pk == partitionKey),
                                  It.Is <string>((id) => id == itemId),
                                  It.IsAny <ItemRequestOptions>(),
                                  It.IsAny <CancellationToken>()))
            .ReturnsAsync(() =>
            {
                var itemResponse = new Mock <ItemResponse <DocumentServiceLeaseCore> >();
                itemResponse.Setup(i => i.Resource).Returns(leaseToUpdate);
                return(itemResponse.Object);
            });

            mockedItems.SetupSequence(i => i.ReplaceItemAsync <DocumentServiceLeaseCore>(
                                          It.Is <string>((id) => id == itemId),
                                          It.Is <DocumentServiceLeaseCore>((lease) => lease == leaseToUpdate),
                                          It.Is <Cosmos.PartitionKey>(pk => pk == partitionKey),
                                          It.IsAny <ItemRequestOptions>(),
                                          It.IsAny <CancellationToken>()))
            .Throws(new CosmosException(string.Empty, HttpStatusCode.PreconditionFailed, 0, string.Empty, 0))
            .Returns(() =>
            {
                var itemResponse = new Mock <ItemResponse <DocumentServiceLeaseCore> >();
                itemResponse.Setup(i => i.Resource).Returns(leaseToUpdate);
                return(Task.FromResult(itemResponse.Object));
            });

            var updater      = new DocumentServiceLeaseUpdaterCosmos(DocumentServiceLeaseUpdaterCosmosTests.GetMockedContainer(mockedItems));
            var updatedLease = await updater.UpdateLeaseAsync(leaseToUpdate, itemId, partitionKey, serverLease =>
            {
                serverLease.Owner = "newHost";
                return(serverLease);
            });

            Assert.AreEqual("newHost", updatedLease.Owner);
            Mock.Get(mockedItems.Object)
            .Verify(items => items.ReplaceItemAsync(It.Is <string>((id) => id == itemId),
                                                    It.Is <DocumentServiceLeaseCore>((lease) => lease == leaseToUpdate),
                                                    It.Is <Cosmos.PartitionKey>(pk => pk == partitionKey),
                                                    It.IsAny <ItemRequestOptions>(),
                                                    It.IsAny <CancellationToken>()), Times.Exactly(2));
            Mock.Get(mockedItems.Object)
            .Verify(items => items.ReadItemAsync <DocumentServiceLeaseCore>(It.Is <Cosmos.PartitionKey>(pk => pk == partitionKey),
                                                                            It.Is <string>((id) => id == itemId),
                                                                            It.IsAny <ItemRequestOptions>(),
                                                                            It.IsAny <CancellationToken>()), Times.Once);
        }
Exemple #4
0
        public async Task ThrowsOnNotFoundReplace()
        {
            string itemId = "1";

            Cosmos.PartitionKey      partitionKey  = new Cosmos.PartitionKey("1");
            DocumentServiceLeaseCore leaseToUpdate = new DocumentServiceLeaseCore();

            Mock <CosmosContainerCore> mockedItems = new Mock <CosmosContainerCore>();

            mockedItems.Setup(i => i.ReadItemAsync <DocumentServiceLeaseCore>(
                                  It.Is <Cosmos.PartitionKey>(pk => pk == partitionKey),
                                  It.Is <string>((id) => id == itemId),
                                  It.IsAny <ItemRequestOptions>(),
                                  It.IsAny <CancellationToken>()))
            .ReturnsAsync(() =>
            {
                var itemResponse = new Mock <ItemResponse <DocumentServiceLeaseCore> >();
                itemResponse.Setup(i => i.Resource).Returns(leaseToUpdate);
                return(itemResponse.Object);
            });

            mockedItems.SetupSequence(i => i.ReplaceItemAsync <DocumentServiceLeaseCore>(
                                          It.Is <string>((id) => id == itemId),
                                          It.Is <DocumentServiceLeaseCore>((lease) => lease == leaseToUpdate),
                                          It.Is <Cosmos.PartitionKey>(pk => pk == partitionKey),
                                          It.IsAny <ItemRequestOptions>(),
                                          It.IsAny <CancellationToken>()))
            .Returns(() =>
            {
                var itemResponse = new Mock <ItemResponse <DocumentServiceLeaseCore> >();
                itemResponse.Setup(i => i.StatusCode).Returns(HttpStatusCode.NotFound);
                return(Task.FromResult(itemResponse.Object));
            })
            .Returns(() =>
            {
                var itemResponse = new Mock <ItemResponse <DocumentServiceLeaseCore> >();
                itemResponse.Setup(i => i.Resource).Returns(leaseToUpdate);
                return(Task.FromResult(itemResponse.Object));
            });

            var updater      = new DocumentServiceLeaseUpdaterCosmos(DocumentServiceLeaseUpdaterCosmosTests.GetMockedContainer(mockedItems));
            var updatedLease = await updater.UpdateLeaseAsync(leaseToUpdate, itemId, partitionKey, serverLease =>
            {
                serverLease.Owner = "newHost";
                return(serverLease);
            });
        }
Exemple #5
0
        public async Task UpdatesLease()
        {
            string itemId = "1";

            Cosmos.PartitionKey      partitionKey  = new Cosmos.PartitionKey("1");
            DocumentServiceLeaseCore leaseToUpdate = new DocumentServiceLeaseCore();

            Stream leaseStream = new CosmosJsonDotNetSerializer().ToStream(leaseToUpdate);

            Mock <ContainerCore> mockedItems = new Mock <ContainerCore>();

            mockedItems.Setup(i => i.ReplaceItemStreamAsync(
                                  It.IsAny <Stream>(),
                                  It.Is <string>((id) => id == itemId),
                                  It.Is <Cosmos.PartitionKey>(pk => pk.Equals(partitionKey)),
                                  It.IsAny <ItemRequestOptions>(),
                                  It.IsAny <CancellationToken>()))
            .ReturnsAsync((Stream stream, string id, PartitionKey pk, ItemRequestOptions options, CancellationToken cancellationToken) =>
            {
                return(new ResponseMessage(HttpStatusCode.OK)
                {
                    Content = stream
                });
            });

            var updater      = new DocumentServiceLeaseUpdaterCosmos(DocumentServiceLeaseUpdaterCosmosTests.GetMockedContainer(mockedItems));
            var updatedLease = await updater.UpdateLeaseAsync(leaseToUpdate, itemId, partitionKey, serverLease =>
            {
                serverLease.Owner = "newHost";
                return(serverLease);
            });

            Assert.AreEqual("newHost", updatedLease.Owner);
            Mock.Get(mockedItems.Object)
            .Verify(items => items.ReplaceItemStreamAsync(
                        It.IsAny <Stream>(),
                        It.Is <string>((id) => id == itemId),
                        It.Is <Cosmos.PartitionKey>(pk => pk.Equals(partitionKey)),
                        It.IsAny <ItemRequestOptions>(),
                        It.IsAny <CancellationToken>()), Times.Once);
            Mock.Get(mockedItems.Object)
            .Verify(items => items.ReadItemStreamAsync(
                        It.Is <string>((id) => id == itemId),
                        It.Is <Cosmos.PartitionKey>((pk) => pk.Equals(partitionKey)),
                        It.IsAny <ItemRequestOptions>(),
                        It.IsAny <CancellationToken>()), Times.Never);
        }
        public async Task UpdatesLease()
        {
            string itemId = "1";

            Cosmos.PartitionKey      partitionKey  = new Cosmos.PartitionKey("1");
            DocumentServiceLeaseCore leaseToUpdate = new DocumentServiceLeaseCore();

            Mock <ContainerCore> mockedItems = new Mock <ContainerCore>();

            mockedItems.Setup(i => i.ReplaceItemAsync <DocumentServiceLeaseCore>(
                                  It.Is <DocumentServiceLeaseCore>((lease) => lease == leaseToUpdate),
                                  It.Is <string>((id) => id == itemId),
                                  It.Is <Cosmos.PartitionKey>(pk => pk == partitionKey),
                                  It.IsAny <ItemRequestOptions>(),
                                  It.IsAny <CancellationToken>()))
            .ReturnsAsync(() =>
            {
                var itemResponse = new Mock <ItemResponse <DocumentServiceLeaseCore> >();
                itemResponse.Setup(i => i.Resource).Returns(leaseToUpdate);
                return(itemResponse.Object);
            });

            var updater      = new DocumentServiceLeaseUpdaterCosmos(DocumentServiceLeaseUpdaterCosmosTests.GetMockedContainer(mockedItems));
            var updatedLease = await updater.UpdateLeaseAsync(leaseToUpdate, itemId, partitionKey, serverLease =>
            {
                serverLease.Owner = "newHost";
                return(serverLease);
            });

            Assert.AreEqual("newHost", updatedLease.Owner);
            Mock.Get(mockedItems.Object)
            .Verify(items => items.ReplaceItemAsync(
                        It.Is <DocumentServiceLeaseCore>((lease) => lease == leaseToUpdate),
                        It.Is <string>((id) => id == itemId),
                        It.Is <Cosmos.PartitionKey>(pk => pk == partitionKey),
                        It.IsAny <ItemRequestOptions>(),
                        It.IsAny <CancellationToken>()), Times.Once);
            Mock.Get(mockedItems.Object)
            .Verify(items => items.ReadItemAsync <DocumentServiceLeaseCore>(
                        It.Is <string>((id) => id == itemId),
                        It.Is <Cosmos.PartitionKey>((pk) => pk == partitionKey),
                        It.IsAny <ItemRequestOptions>(),
                        It.IsAny <CancellationToken>()), Times.Never);
        }