Beispiel #1
0
        public async Task PopulateMissingRange()
        {
            DocumentServiceLeaseStoreManagerOptions options = new DocumentServiceLeaseStoreManagerOptions
            {
                HostName = Guid.NewGuid().ToString()
            };

            DocumentServiceLeaseCore lease = new DocumentServiceLeaseCore()
            {
                LeaseToken = "0",
                Owner      = Guid.NewGuid().ToString()
            };

            Mock <DocumentServiceLeaseUpdater> mockUpdater = new Mock <DocumentServiceLeaseUpdater>();

            Func <Func <DocumentServiceLease, DocumentServiceLease>, bool> validateUpdater = (Func <DocumentServiceLease, DocumentServiceLease> updater) =>
            {
                // Simulate dirty read from db
                DocumentServiceLease afterUpdateLease = updater(lease);
                return(afterUpdateLease.FeedRange != null);
            };

            mockUpdater.Setup(c => c.UpdateLeaseAsync(
                                  It.IsAny <DocumentServiceLease>(),
                                  It.IsAny <string>(),
                                  It.IsAny <PartitionKey>(),
                                  It.Is <Func <DocumentServiceLease, DocumentServiceLease> >(f => validateUpdater(f))))
            .ReturnsAsync(lease);

            Mock <ContainerInternal>   containerMock = new Mock <ContainerInternal>();
            Mock <CosmosClientContext> mockContext   = new Mock <CosmosClientContext>();

            containerMock.Setup(c => c.ClientContext).Returns(mockContext.Object);
            containerMock.Setup(c => c.LinkUri).Returns("http://localhot");
            containerMock.Setup(c => c.GetCachedRIDAsync(It.IsAny <bool>(), It.IsAny <CancellationToken>())).ReturnsAsync("test");
            MockDocumentClient mockDocumentClient = new MockDocumentClient();

            mockContext.Setup(c => c.DocumentClient).Returns(mockDocumentClient);

            DocumentServiceLeaseManagerCosmos documentServiceLeaseManagerCosmos = new DocumentServiceLeaseManagerCosmos(
                containerMock.Object,
                Mock.Of <ContainerInternal>(),
                mockUpdater.Object,
                options,
                Mock.Of <RequestOptionsFactory>());

            DocumentServiceLease afterAcquire = await documentServiceLeaseManagerCosmos.AcquireAsync(lease);

            Assert.IsNotNull(afterAcquire.FeedRange);
        }
Beispiel #2
0
        public async Task AcquireCompletes()
        {
            DocumentServiceLeaseStoreManagerOptions options = new DocumentServiceLeaseStoreManagerOptions
            {
                HostName = Guid.NewGuid().ToString()
            };

            DocumentServiceLeaseCore lease = new DocumentServiceLeaseCore()
            {
                LeaseToken = "0",
                Owner      = Guid.NewGuid().ToString(),
                FeedRange  = new FeedRangePartitionKeyRange("0")
            };

            Mock <DocumentServiceLeaseUpdater> mockUpdater = new Mock <DocumentServiceLeaseUpdater>();

            Func <Func <DocumentServiceLease, DocumentServiceLease>, bool> validateUpdater = (Func <DocumentServiceLease, DocumentServiceLease> updater) =>
            {
                DocumentServiceLease afterUpdateLease = updater(lease);
                return(options.HostName == afterUpdateLease.Owner);
            };

            mockUpdater.Setup(c => c.UpdateLeaseAsync(
                                  It.IsAny <DocumentServiceLease>(),
                                  It.IsAny <string>(),
                                  It.IsAny <PartitionKey>(),
                                  It.Is <Func <DocumentServiceLease, DocumentServiceLease> >(f => validateUpdater(f))))
            .ReturnsAsync(lease);

            DocumentServiceLeaseManagerCosmos documentServiceLeaseManagerCosmos = new DocumentServiceLeaseManagerCosmos(
                Mock.Of <ContainerInternal>(),
                Mock.Of <ContainerInternal>(),
                mockUpdater.Object,
                options,
                Mock.Of <RequestOptionsFactory>());

            DocumentServiceLease afterAcquire = await documentServiceLeaseManagerCosmos.AcquireAsync(lease);

            Assert.AreEqual(options.HostName, afterAcquire.Owner);
        }