Esempio n. 1
0
        public async Task ReleaseInitializationLockAsync_ShouldPassPartitionKey_IfLeaseCollectionPartitioned()
        {
            var client = Mock.Of <IChangeFeedDocumentClient>();

            Mock.Get(client)
            .Setup(c => c.DeleteDocumentAsync(It.IsAny <Uri>(), It.IsAny <RequestOptions>(), default(CancellationToken)))
            .ReturnsAsync(new ResourceResponse <Document>(new Document()));

            var pkValue = "pk";
            var requestOptionsFactory = Mock.Of <IRequestOptionsFactory>();

            Mock.Get(requestOptionsFactory)
            .Setup(factory => factory.CreateRequestOptions(It.IsAny <ILease>()))
            .Returns(new RequestOptions {
                PartitionKey = new PartitionKey(pkValue)
            });

            var  leaseStore             = new DocumentServiceLeaseStore(client, collectionInfo, containerNamePrefix, leaseCollectionLink, requestOptionsFactory);
            bool isLockFoundAndReleased = await leaseStore.ReleaseInitializationLockAsync();

            Assert.True(isLockFoundAndReleased);

            Mock.Get(client)
            .Verify(c => c.DeleteDocumentAsync(
                        It.Is <Uri>(uri => uri.OriginalString.EndsWith("prefix.lock")),
                        It.Is <RequestOptions>(options => new PartitionKey(pkValue).Equals(options.PartitionKey)),
                        default(CancellationToken)));
        }
Esempio n. 2
0
        public async Task ReleaseInitializationLockAsync_PassesOverETag()
        {
            var etag = "etag";

            var client = Mock.Of <IChangeFeedDocumentClient>();

            Mock.Get(client)
            .Setup(c => c.CreateDocumentAsync(leaseCollectionLink, It.IsAny <object>(), null, false, default(CancellationToken)))
            .ReturnsAsync(new ResourceResponse <Document>(JsonConvert.DeserializeObject <Document>($"{{\"_etag\":\"{etag}\"}}")));
            Mock.Get(client)
            .Setup(c => c.DeleteDocumentAsync(It.IsAny <Uri>(), It.IsAny <RequestOptions>(), default(CancellationToken)))
            .ReturnsAsync(new ResourceResponse <Document>(new Document()));

            var leaseStore = new DocumentServiceLeaseStore(client, collectionInfo, containerNamePrefix, leaseCollectionLink, Mock.Of <IRequestOptionsFactory>());

            bool isLockAcquired = await leaseStore.AcquireInitializationLockAsync(lockTime);

            Assert.True(isLockAcquired);

            bool isLockFoundAndReleased = await leaseStore.ReleaseInitializationLockAsync();

            Assert.True(isLockFoundAndReleased);

            Mock.Get(client)
            .Verify(c => c.DeleteDocumentAsync(
                        It.IsAny <Uri>(),
                        It.Is <RequestOptions>(options => options.AccessCondition.Type == AccessConditionType.IfMatch && options.AccessCondition.Condition == etag),
                        default(CancellationToken)),
                    Times.Once);
        }
Esempio n. 3
0
        public async Task ReleaseInitializationLockAsync_ShouldThrow_IfLockThrows()
        {
            var client = Mock.Of <IChangeFeedDocumentClient>();

            Mock.Get(client)
            .Setup(c => c.DeleteDocumentAsync(It.IsAny <Uri>(), It.IsAny <RequestOptions>(), default(CancellationToken)))
            .ThrowsAsync(DocumentExceptionHelpers.CreateRequestRateTooLargeException());

            var       leaseStore = new DocumentServiceLeaseStore(client, collectionInfo, containerNamePrefix, leaseCollectionLink, Mock.Of <IRequestOptionsFactory>());
            Exception exception  = await Record.ExceptionAsync(() => leaseStore.ReleaseInitializationLockAsync());

            Assert.IsAssignableFrom <DocumentClientException>(exception);
        }
Esempio n. 4
0
        public async Task ReleaseInitializationLockAsync_ShouldReturnFalse_IfLockNotFound()
        {
            var client = Mock.Of <IChangeFeedDocumentClient>();

            Mock.Get(client)
            .Setup(c => c.DeleteDocumentAsync(It.IsAny <Uri>(), It.IsAny <RequestOptions>(), default(CancellationToken)))
            .ThrowsAsync(DocumentExceptionHelpers.CreateNotFoundException());

            var  leaseStore             = new DocumentServiceLeaseStore(client, collectionInfo, containerNamePrefix, leaseCollectionLink, Mock.Of <IRequestOptionsFactory>());
            bool isLockFoundAndReleased = await leaseStore.ReleaseInitializationLockAsync();

            Assert.False(isLockFoundAndReleased);

            Mock.Get(client)
            .Verify(c => c.DeleteDocumentAsync(It.Is <Uri>(uri => uri.OriginalString.EndsWith("prefix.lock")), It.IsAny <RequestOptions>(), default(CancellationToken)), Times.Once);
        }